문제

I wrote a small CLI program to delete specific Safari cookies for me. Functionally it's fine, but it's throwing up warnings about objects being "autoreleased with no pool in place". My project has ARC enabled, hence why I don't have any autorelease pools.

Here's my code:

// 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;

}

The message I get is:

objc[15502]: Object 0x107b2bf00 of class NSThread autoreleased with no pool in place - just leaking - break on objc_autoreleaseNoPool() to debug

Which appears in the Xcode debugger or when running the release binary directly (slight digression: shouldn't these messages be stripped out of the "release" build?). The line that causes it seems to be:

NSHTTPCookieStorage *cookieStorage = [NSHTTPCookieStorage sharedHTTPCookieStorage];

Similarly, if I run it without passing an argument, I get a similar message:

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

Which appears to come from the IFPrint function I'm using (however this doesn't show up when I use the IFPrint when I provide a proper argument).

I'm a bit out of my depth here, can anyone show me where (and how) I've gone wrong?

도움이 되었습니까?

해결책

ARC still requires an autorelease pool. Methods like [NSPredicate predicateWithFormat:filterString] continue to release an autoreleased object (though you no longer need to concern yourself all that much since ARC handles it). Furthermore the internal implementation of any library method you call may create arbitrarily many autoreleased objects while running.

You should wrap your code in an autorelease pool via the @autoreleasepool mechanism.

다른 팁

Wrap the entire body of main with @autoreleasepool like so:

int main(int argc, const char * argv[])
{
  @autoreleasepool 
  {
    // your code
  }
}

All you need to do is add an autoreleasepool in your main.

int main(int argc, const char * argv[])
{ 
     @autoreleasepool
     {
         //Your code
     }
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top