Re: Newbie question: autorelease sends autorelease
Re: Newbie question: autorelease sends autorelease
- Subject: Re: Newbie question: autorelease sends autorelease
- From: Ondra Cada <email@hidden>
- Date: Wed, 22 Sep 2004 18:41:59 +0200
James,
On 22.9.2004, at 18:15, James Bucanek wrote:
In my case, I was autoreleasing in the dealloc handler because the
object hands out lots of references to the child object and I was
afraid of releasing it immediately.
In retrospect, this was misguided. Any client that wants to hang onto
one of the children should retain it, and I should be releasing the
object in the dealloc handler.
Well, it is a bit more complicated than that. The question is, (a) how
does the getter work, and (b) whether the pattern
id o=[vendor getter];
// code here
[o whatever];
is considered valid (which is kind of a fighting point and I won't
discuss it anymore: just I say I think it generally should be. There
are others who do not think so).
The trick is, in case "code here"'s sideeffect happens to release the
vendor, the [o whatever] may crash. There are the following
possibilities to prevent it:
(a) consistently use "id o=[[[vendor getter] retain] autorelease]" (or
the "retain/code here/release" pattern with its own drawbacks, as
discussed in my previous post) in these cases;
(b) use an autoreleasing getter which does that for you automatically
(ie. "getter" is implemented like "-getter{return [[foo retain]
autorelease];}");
(c) use autorelease in dealloc (and also in the setter).
Note that (a) means you do the same thing at umpteen places instead at
one common place, which is generally considered bad; (b) can get pretty
inefficient; (c) is not 100% safe (it may fail in case a nested pool is
used). The choice is yours :)))
The current oficial Apple guidelines recommend (b) unless there are
considerable speed issues. Again, there are people who think those
guidelines are wrong.
I do recommend searching list archives for a long and somewhat pretty
heated discussion about accessors, namely autoreleasing getters and
setters, to get a better image of this.
---
Ondra Čada
OCSoftware: email@hidden http://www.ocs.cz
private email@hidden http://www.ocs.cz/oc
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden