NsAutoreleasePool

Autorelease pool handles the autoreleased objects. If you own an object (via alloc or copy or retain) then you must release it.

An autorelease pool is created for the main thread. (You can look into the main function if you want). Every thread need its own autorelease pool to manage autoreleased objects. That’s why if you create another thread then you must create an autorelease pool for that thread. Even if you don’t create autoreleased object in the thread, you should create this as the library calls in that thread may create autoreleased objects. Even if you are sure that no library calls are making autoreleased objects then you also should create them as that is the best practice, specially if you are working on big project which is developed and maintained by multiple people.

///////////////////////////////////////////////////////////

-(void) doSomething {


   NSAutorelease *pool = [[NSAutorelasepool alloc] init];

   NSString *myString = [[[NSString alloc] initWithString:@"Hello"] autorelease];

   // or create string like this (automatically autoreleased)
   NSString *myString = [NSString stringWithString:@"Hello"];

   [pool release];
}

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s