我有以下简单的“检查我的应用程序是否设置为在登录时启动”代码。它在垃圾收集下运行良好。但是,由于我开始使用 ARC(并根据需要插入“ __bridge
”),代码开始随机且不可预测地崩溃。根据堆栈跟踪,代码在某些CFRelease
. 任何想法可能导致在 ARC 下发生这种情况?
- (BOOL)loginItemExists
{
NSString *appPath = [[NSBundle mainBundle] bundlePath];
LSSharedFileListRef loginItems = LSSharedFileListCreate(NULL,
kLSSharedFileListSessionLoginItems, NULL);
BOOL found = NO;
UInt32 seedValue;
CFURLRef thePath;
CFArrayRef loginItemsArray = LSSharedFileListCopySnapshot(loginItems,
&seedValue);
for (id item in (__bridge NSArray *)loginItemsArray)
{
LSSharedFileListItemRef itemRef = (__bridge LSSharedFileListItemRef)item;
if (LSSharedFileListItemResolve(itemRef, 0, &thePath, NULL) == noErr)
{
if ([[(__bridge NSURL *)thePath path] hasPrefix:appPath])
found = YES;
}
//docs for LSSharedFileListItemResolve say we should release the CFURLRef
if (thePath != NULL)
CFRelease(thePath);
if (found)
break;
}
CFRelease(loginItemsArray);
CFRelease(loginItems);
return found;
}