Re: retain and autorelease
Re: retain and autorelease
- Subject: Re: retain and autorelease
- From: Glen Simmons <email@hidden>
- Date: Fri, 14 Nov 2003 16:34:06 -0600
On Nov 14, 2003, at 3:26 PM, Ben Dougall wrote:
On Thursday, November 13, 2003, at 11:29 pm, Shawn Erickson wrote:
On Nov 12, 2003, at 7:25 PM, Darwin Zins wrote:
When I do this:
ABPerson * newMacABEntry;
newMacABEntry = (ABPerson *) [_macAddressBook recordForUniqueId:
abID ];
Should I then do: [newMacABEntry retain]; and when I am done with it
[newMacABEntry autorelease];
It depends on what you are doing. In general if you are not using the
entry outside of the method you are currently in then you need not
retain it. It will remain for your use while inside of your method,
assuming you (or possibly someone else) do nothing to get it released
earlier.
that's something i've never been sure on - the lifetime of an
unretained object like that. you say it'll probably hang round until
the end of the current method - what about if there's a method call
inbetween the above non retained convenience creation and the end of
the current method? even a simple presumably short/quick method call
like adding an object to an array for exmaple? that takes the thread
elsewhere, out of the current method. would it still be ok not to
retain do you think?
Unless your app is multi-threaded, the lifetime of an autoreleased
object is well-defined - unless it's retained, it will be released when
the current execution stack returns to the run-loop and the autorelease
pool is dealloced. So, you can be sure that an autoreleased object will
be around for the rest of the current method. If you need to hold on to
it for longer, you retain it.
When you start dealing with multiple threads, you also have multiple
autorelease pools (one for each thread). There is the potential for an
object to be added to another threads pool and get released before the
method you are in returns. You would have to try a bit to accomplish
this, so it's not anything to normally worry about.
HTH,
Glen
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.