Re: Memory management question (passing objects to method)
Re: Memory management question (passing objects to method)
- Subject: Re: Memory management question (passing objects to method)
- From: Tim Lucas <email@hidden>
- Date: Fri, 4 Mar 2005 09:20:31 +1100
On 03/03/2005, at 4:16 PM, Jeff Laing wrote:
We aren't returning values from these things, we are talking about
memory management (retain/release/init/dealloc).
Well, in this specific case, the example code relied on the return
value
from retain. So it seems to me that it was relevant.
It was relying on the return value of nil for setting an object
pointer, which, by itself, there is nothing wrong with.
So by your theory beginners should do memory management throughout
their class, while the rest of us keep the memory management of our
instance vars contained within our accessors?
No, not at all. Personally, I think beginners should be doing
if (aString != value) {
[value release];
value = aString;
if (value) [value retain];
}
and that, in my *personal* opinion, is clearer to the reader that
there will
be cases where aString will be nil.
I see absolutely no advantage in adding the if statement above (apart
from the cost of a message as you pointed out), and, whilst I
appreciate expressive code, I would never add this just as a reminder
that object pointers may be nil when a comment would a much clearer
reminder.
[msg snip]
Even having had:
if (aString != value) {
[value release];
value = [aString retain]; // note: value = nil if aString == nil
}
would have improved the sample, for the beginner, who is known to be
goofing
up his retain/release calls!
Agreed.
[msg snip]
My only hope is that people will understand that, when
responding to people asking these sort of basic questions on a mailing
list,
very few of them are ready for the super-optimised solutions, and they
may
learn as many bad habits as good.
My understanding was that it was a good practice, and beginners would
question, as I did when I first time I saw this technique, why it is
that it works.
Use it or not, I hope somebody has learnt something new about objc. It
may have been incorrect to call it a best practice.
What I said was that the accessor as presented still contained subtle
ambiguities that most beginners would miss.
Well they definitely know now =)
- tim lucas
_______________________________________________
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