Question

Can anyone tell me why is NSAutoreleasePool leaking in this code. I am using instruments to check leaks & this code is somehow leaking. Can anyone guide me to the right direction. I have the exact same code elsewhere & its running fine.

ASIHTTPRequest *request = [[ASIHTTPRequest alloc] initWithURL:self.url];
 [request setDelegate:self];

 NSOperationQueue *queue = self.downloadQueue;
 [queue addOperation:request];
 [request release];
Was it helpful?

Solution

It should go away if you create an NSAutoreleasePool around that code. This usually happens if this code is not being run on the main thread.

NSAutoreleasePool* pool = [NSAutoreleasePool new];
// Your code goes here
[pool drain];

Also, be aware that if you are looping inside an NSAutoreleasePool new/drain block, you will appear to leak until you hit the drain. The upshot of this is if you are allocating too much memory inside the loop your app can still run out of memory. In that case you may need to create and drain a second NSAutoreleasePool every time through the loop (or possibly every nth time through the loop to avoid some of the overhead).

OTHER TIPS

Are you running this code on a secondary thread? If so did you setup an autorelease pool on entry?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top