views:

59

answers:

1

Instrument reports leak for this simple use of NSURLConnection:

#import <Foundation/Foundation.h>
int main (int argc, char ** argv)
{
    NSAutoreleasePool *pool = [NSAutoreleasePool new];
    NSURLRequest *theRequest = [NSURLRequest
        requestWithURL:[NSURL URLWithString:@"https://gmail.com/"]
        cachePolicy:NSURLRequestReloadIgnoringLocalCacheData
        timeoutInterval:60.0];
    [NSURLConnection connectionWithRequest:theRequest delegate:nil];
    [[NSRunLoop currentRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:5]];
    [pool drain];
    [NSThread sleepForTimeInterval:10]; // wait for Instruments to check
}

Leak stack trace:

   0 CoreFoundation __CFBasicHashRehash
   1 CoreFoundation CFDictionaryCreate
   2 CFNetwork _getConnectionInfoForProxy
   3 CFNetwork HTTPProtocol::createStream()
   4 CFNetwork HTTPProtocol::createAndOpenStream()
   5 CFNetwork executionContextPerform(void*)
   6 CoreFoundation __CFRunLoopDoSources0
   7 CoreFoundation __CFRunLoopRun
   8 CoreFoundation CFRunLoopRunSpecific
   9 CoreFoundation CFRunLoopRunInMode
  10 Foundation +[NSURLConnection(NSURLConnectionReallyInternal) _resourceLoadLoop:]
  11 Foundation -[NSThread main]
  12 Foundation __NSThread__main__
  13 libSystem.B.dylib _pthread_start
  14 libSystem.B.dylib thread_start

It doesn't leak if the URL is just normal http and it doesn't redirect to a https site.

How do I fix the leak?

I'm using Snow Leopard and I have proxies on for both http and https.

A: 

I think you're asking the wrong questions. Here's what you should be asking:

  1. Am I following the memory management rules?
  2. If I am, do I need to worry about this?

In your case, the answers are (respectively) "Yes" and "No". It's quite possible that you have found a memory leak in the Cocoa frameworks. However in this case, I don't think you have. You see, the NSRunLoop for the current thread retains the NSURLConnection, so it's possible that you're just not giving the run loop enough time to release the connection or something.

The point is, you're not doing anything wrong, so Don't Panic.

Dave DeLong
It doesn't matter how much time I give to the run-loop. It still leaks.
David Lin
@davidlin so? *you* haven't done anything wrong, so the leak (if it's a real leak) is not your problem and may be fixed in a forthcoming system updated. trying to code around it now would be a headache and may break your code in the future when it may (or may not) get fixed.
Dave DeLong
@davidlin also, I ran the code you provided above and Instruments did not report a leak.
Dave DeLong
Thanks for trying it out, Dave. I did some more experiments and I updated the question with more information. I think it has something to do with https + proxy. I'm looking for a workaround if it's a bug in Apple's code since my app does a lot of https requests.
David Lin
Also, if _getConnectionInfoForProxy is in CFNetwork, shouldn't it be open sourced? I couldn't find this function anywhere.
David Lin