Friday, April 8, 2011

Autoreleased with No Pool in Place - Just Leaking

When you perform an operation in another thread, place EVERYTHING inside your thread specific autorelease pool. That includes writing debug messages in log as well as sending notifications. Otherwise you will leak memory.
2011-04-08 06:54:45.665 MyApp[6278:5f03] *** __NSAutoreleaseNoPool(): Object 0x7050800 of class UITableView autoreleased with no pool in place - just leaking
2011-04-08 06:54:45.665 MyApp[6278:5f03] *** __NSAutoreleaseNoPool(): Object 0x7926260 of class __NSDate autoreleased with no pool in place - just leaking
2011-04-08 06:54:45.666 MyApp[6278:5f03] *** __NSAutoreleaseNoPool(): Object 0x7926150 of class __NSCFTimer autoreleased with no pool in place - just leaking
Here's a sample how to do it:
- (IBAction)newStuff:(id)sender
{
[[MyThing sharedThing]
performSelectorInBackground:
@selector(doStuff) withObject:nil];
}

- (void)doStuff
{
NSAutoreleasePool * pool =
[[NSAutoreleasePool alloc] init];
NSLog(@"start");

//... your code here

NSLog(@"ready");
[[NSNotificationCenter defaultCenter]
postNotificationName:StuffReadyNotification
object:nil];
[pool release];
}
Don't try to optimize until you really need to.

No comments:

Post a Comment