我有一个加载到现有 Carbon 应用程序中的 Cocoa 插件。
首次加载插件时,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 应用程序不会发送任何通知。
是否真的有必要清理我在应用程序终止时创建的“全局”自动释放池?
我尝试通过添加对以下函数的调用来注册 Carbon 应用程序退出事件registerForApplicationQuitNotification()
,但是当应用程序终止时,我收到警告说我正在调用-release
无效的自动释放池。我处理 Carbon 事件的方式有问题吗?
//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);
}