Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CFNetwork / NSURLConnection leak

Running instruments on the device, I intermittently incur a memory leak of exactly 3.5 KB in CFNetwork, the responsible frame being "HostLookup_Master::HostLookup...."

I have read a number of questions re this issue and have separately tried the following to fix the leak:

  1. Included the following in applicationDidFinishLaunching:

    NSURLCache *sharedCache = [[NSURLCache alloc] initWithMemoryCapacity:0 diskCapacity:0 diskPath:nil]; [NSURLCache setSharedURLCache:sharedCache]; [sharedCache release];

  2. Specified in the urlrequest not to load from the local cache.

None of the above worked. My class that instantiates the connections does not leak as its instances are released when data has been downloaded. I have verified this by confirming the the living objects of the class is 0 using Instruments.

Any advice on addressing this leak would be greatly appreciated.

like image 908
RunLoop Avatar asked Mar 13 '10 16:03

RunLoop


2 Answers

That 3.5kb memory leak sounds familiar, had that when dealing with threads:

@implementation MyClass
+ (void)login
{
    //MyClass *this = [[MyClass alloc] init]; // MEMORY LEAK
    MyClass *this = [[[MyClass alloc] init] autorelease];
    [NSThread detachNewThreadSelector:@selector(anotherThread)
                             toTarget:this
                           withObject:nil];
}

- (void)anotherThread {
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
    [self doStuff];
    //[self release]; // MEMORY LEAK
    [pool release];
}
@end

Every login created 3.5kb leak. Using autorelease solved the problem.

like image 138
JOM Avatar answered Oct 22 '22 15:10

JOM


It seems that apple might be aware of the 3.5k leak related to CFNetwork usage and may have been reported as a bug already.

like image 41
ROGUE Avatar answered Oct 22 '22 15:10

ROGUE