When the auto-ad pool starts

I used autorelease throughout the application and would like to understand the behavior of the auto-advertising method. When does the autostart pool merge by default? is it based on a timer (every 30 seconds?) or should it be called manually? do i need to do something to release the variables tagged with autoadvertising?

+4
source share
2 answers

There are (I would say) 3 main instances when they are created and released:

1. The beginning and very end of the life cycle of your application, written in main.m

int main(int argc, char *argv[]) { NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init]; int retVal = UIApplicationMain(argc, argv, nil, nil); [pool release]; return retVal; } 

2. The beginning and end of each event (done in AppKit)

 NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init]; - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil - (void)loadView /* etc etc initialization stuff... */ [pool release]; 

3. When you want (you can create your own pool and free it. [From apple memory management document])

 – (id)findMatchingObject:anObject { id match = nil; while (match == nil) { NSAutoreleasePool *subPool = [[NSAutoreleasePool alloc] init]; /* Do a search that creates a lot of temporary objects. */ match = [self expensiveSearchForObject:anObject]; if (match != nil) { [match retain]; /* Keep match around. */ } [subPool release]; } return [match autorelease]; /* Let match go and return it. */ } 
+5
source

It is reset whenever the current run loop ends. This is when your method and method calling your method and method calling this method, etc., are executed.

From the documentation :

A set of applications creates an autostart pool in the main thread at the beginning of each cycle of the event loop and depletes it at the end, thereby freeing up any objects with autorealysis generated during event processing

+5
source

All Articles