I have a Cocoa plug-in that is loaded into an existing Carbon application.
When the plug-in is first loaded, the Carbon application calls an initialization function, Plugin_Init()
and in that function I set up the environment like so:
//this is the global autorelease pool
static NSAutoreleasePool* globalPool = nil;
void Plugin_Init()
{
NSApplicationLoad(); //loads the Cocoa event loop etc
//create an autorelease pool
globalPool=[[NSAutoreleasePool alloc] init];
//callback functions are registered here
Plugin_defineFunction("doSomething",doSomething,0);
}
However, the Carbon app does not send any notifications when the application is about to terminate.
Is it actually necessary to clean up the "global" autorelease pool that I've created when the app terminates?
I tried registering for the Carbon app quit event by adding a call to the registerForApplicationQuitNotification()
function below, but when the application terminated I received warnings that I was calling -release
on an invalid autorelease pool. Is there a problem with how I'm handling the Carbon events?
//handles the Carbon application quit notification
static pascal OSStatus handleApplicationQuitEvent(EventHandlerCallRef nextHandler, EventRef evt, void *ud)
{
OSStatus err = noErr;
UInt32 evtkind;
evtkind = GetEventKind( evt );
if ( evtkind == kEventAppQuit )
{
//release the global autorelease pool
[globalPool release];
}
// call the rest of the handlers
err = CallNextEventHandler( nextHandler, evt);
return err;
}
//registers for the Carbon application quit notification
void registerForApplicationQuitNotification()
{
// install an event handler to tear down some globals on Quit
static EventHandlerUPP app = NULL;
EventTypeSpec list[] = {
{kEventClassApplication, kEventAppQuit},
};
app = NewEventHandlerUPP( handleApplicationQuitEvent );
if (!app)
return;
InstallApplicationEventHandler(app, GetEventTypeCount(list), list, NULL, NULL);
}