我有以下简单的“检查我的应用程序是否设置为在登录时启动”代码。它在垃圾收集下工作得很好。然而,自从我开始使用 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;
}
如果该项目未解析,您将在第二次循环中获得双重释放。在开始时以及释放后将 Path 设置为 nil,这将修复崩溃。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)