我编写了一个小型 CLI 程序来为我删除特定的 Safari cookie。在功能上它很好,但它会抛出关于对象“自动释放而没有到位池”的警告。我的项目启用了 ARC,因此我没有任何自动释放池。
这是我的代码:
// NSLog replacement from http://stackoverflow.com/a/3487392/1376063
void IFPrint (NSString *format, ...) {
va_list args;
va_start(args, format);
fputs([[[NSString alloc] initWithFormat:format arguments:args] UTF8String], stdout);
fputs("\n", stdout);
va_end(args);
}
int main(int argc, const char * argv[])
{
NSString *urlSearchString;
if (argc > 1) {
urlSearchString = [[NSString alloc] initWithUTF8String:argv[1]];
}
else {
IFPrint(@"No URL provided, quitting.");
return 1;
}
NSHTTPCookieStorage *cookieStorage = [NSHTTPCookieStorage sharedHTTPCookieStorage];
NSString *filterString = [[NSString alloc] initWithFormat:@"domain ENDSWITH '%@'", urlSearchString];
NSPredicate *filter = [NSPredicate predicateWithFormat:filterString];
NSArray *matchedCookies = [cookieStorage.cookies filteredArrayUsingPredicate:filter];
for (int i = 0; i < matchedCookies.count; i++) {
[cookieStorage deleteCookie:[matchedCookies objectAtIndex:i]];
}
IFPrint(@"Removed %li cookies", matchedCookies.count);
return 0;
}
我得到的信息是:
objc[15502]: Object 0x107b2bf00 of class NSThread autoreleased with no pool in place - just leaking - break on objc_autoreleaseNoPool() to debug
哪个出现在 Xcode 调试器中或直接运行发布二进制文件时(稍微题外话:这些消息不应该从“发布”构建中剥离吗?)。导致它的行似乎是:
NSHTTPCookieStorage *cookieStorage = [NSHTTPCookieStorage sharedHTTPCookieStorage];
同样,如果我在不传递参数的情况下运行它,我会收到类似的消息:
objc[15630]: Object 0x100114ed0 of class __NSCFString autoreleased with no pool in place - just leaking - break on objc_autoreleaseNoPool() to debug
objc[15630]: Object 0x100114f80 of class __NSCFData autoreleased with no pool in place - just leaking - break on objc_autoreleaseNoPool() to debug
这似乎来自IFPrint
我正在使用的函数(但是IFPrint
当我提供正确的参数时,这并没有出现)。
我在这里有点超出我的深度,谁能告诉我我在哪里(以及如何)出错了?