• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: create an autorelease pool in the init-method and release it in the dealloc-method ..?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: create an autorelease pool in the init-method and release it in the dealloc-method ..?


  • Subject: Re: create an autorelease pool in the init-method and release it in the dealloc-method ..?
  • From: Dave Zarzycki <email@hidden>
  • Date: Sat, 14 May 2011 18:41:08 -0700

Here is a trick to help you better understand how to use autorelease pools correctly. Put this in a header somewhere:

static inline void subpool(void (^block)(void))
{
	NSAutoreleasePool *_pool = [[NSAutoreleasePool alloc] init];
	block();
	[pool drain];
}

… and use it like so:

subpool(^{
	// do stuff
});

And if you need to get stuff out of a subpool:

__block NSThingy *t = nil;
subpool(^{
	t = [other getThingy];
	[t retain]; // because getThingy might return autoreleased
});
[t doSomething];
[t release]; // balance the earlier retain
t = nil;



On May 14, 2011, at 6:19 PM, Nick Zitzmann wrote:

>
> On May 14, 2011, at 5:43 PM, Martin Batholdy wrote:
>
>> I still have trouble understanding the autorelease pool.
>>
>> Lets assume an object Z has a method where it gets a string y and returns another string x.
>>
>> Now when an instance of this object is created and the method is invoked,
>> x is returned and is used somewhere else.
>>
>> Now this method of object Z should not be the owner of x right?
>> Because x is used elsewhere.
>>
>>
>> So I add x to the autorelease-pool to declare that I am not the owner (and won't send it a release message);
>>
>> x = [[[NSString alloc] init] autorelease];
>>
>>
>> but where do I release the pool?
>> And to which pool is it added?
>
> Pay attention to the memory management rules: <http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/MemoryMgmt/Articles/mmRules.html>
>
>> I thought I would create my own pool in the init-method of Z and send it a [pool release] message in the alloc-method.
>>
>> But I haven't seen that so far in sample code ...
>
> Don't ever do that. You'll wind up popping objects that aren't supposed to be popped yet, which will lead to a crash. Autorelease pools should never be used as ivars, globals, or static variables.
>
> In general, you shouldn't create your own autorelease pools, unless (1) you are using NSThread or pthread to create new threads, or (2) you have written a loop that generates a great deal of temporary objects that need to be popped from time to time to keep memory usage from spiking, and you know what you're doing.
>
>> can someone explain me what happens when I add x to the autorelease pool?
>
> Read the autorelease pools article in the memory management programming guide (see link above) and it will clear things up.
>
> Nick Zitzmann
> <http://www.chronosnet.com/>
>
>
>
> _______________________________________________
>
> Cocoa-dev mailing list (email@hidden)
>
> Please do not post admin requests or moderator comments to the list.
> Contact the moderators at cocoa-dev-admins(at)lists.apple.com
>
> Help/Unsubscribe/Update your Subscription:
>
> This email sent to email@hidden

_______________________________________________

Cocoa-dev mailing list (email@hidden)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:

This email sent to email@hidden

References: 
 >create an autorelease pool in the init-method and release it in the dealloc-method ..? (From: Martin Batholdy <email@hidden>)
 >Re: create an autorelease pool in the init-method and release it in the dealloc-method ..? (From: Nick Zitzmann <email@hidden>)

  • Prev by Date: Re: create an autorelease pool in the init-method and release it in the dealloc-method ..?
  • Next by Date: Tab Bar Application
  • Previous by thread: Re: create an autorelease pool in the init-method and release it in the dealloc-method ..?
  • Next by thread: Re: create an autorelease pool in the init-method and release it in the dealloc-method ..?
  • Index(es):
    • Date
    • Thread