我有一个Cocoa插件,它被加载到现有的Carbon应用程序中。
首次加载插件时,Carbon应用程序调用初始化函数,
Plugin_Init()
在这个函数中,我建立了这样的环境:
//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);
}
但是,当应用程序即将终止时,Carbon应用程序不会发送任何通知。
是否有必要清理应用程序终止时创建的“全局”自动释放池?
我尝试注册碳应用程序退出事件,将呼叫添加到
registerForApplicationQuitNotification()
下面的函数,但是当应用程序终止时,我收到了我正在调用的警告
-release
在无效的自动释放池上。我处理碳排放事件的方式有问题吗?
//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);
}