Re: create an autorelease pool in the init-method and release it in the dealloc-method ..?
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: Martin Batholdy <email@hidden>
- Date: Sun, 15 May 2011 04:20:37 +0200
thanks!
Perhaps my confusion comes from reading about autorelease pools in an objective-c books that focuses on command-line program.
So if I understood it correctly;
(based on <http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/MemoryMgmt/Articles/mmAutoreleasePools.html#//apple_ref/doc/uid/20000047-CJBFBEDI>)
whenever I add an object to the autorelease pool or don't own the object it gets released automatically after an event cycle.
is that correct?
So I only have to worry about objects I own, that are objects I allocate with alloc
(I don't use new or copy anywhere).
On 15.05.2011, at 03:19, 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