Re: Memory Management
Re: Memory Management
- Subject: Re: Memory Management
- From: Marcel Weiher <email@hidden>
- Date: Mon, 28 Jul 2003 21:12:59 +0100
On Monday, Jul 28, 2003, at 20:50 Europe/London, Fritz Anderson wrote:
[autoreleasing accessor]
Although there has been some championing of this method by
well-placed individuals, their preference has not met with an
overwhelmingly positive response, to say the least. IMNSHO, their
case, although appealing at first, has been thoroughly debunked and
is not widely supported by the Cocoa community any longer. Of
course, you may disagree with this view, but at the very least the
statement that this is the preferred method is highly controversial.
You surprise me.
I do that.
I once ran into a string-processing method in OmniFoundation which,
in no-op cases, returned its internal storage.
Fine thing to do.
Consider the following code:
NSString * result = [ofProcessingObject processedString];
[ofProcessObject release];
NSLog(@"The result is %@", result);
I am entitled to assume that result has not been disposed-of, am I not?
Not at all. What makes you think you are entitled to this assumption?
As it turns out, I'm right in cases where -processString had to
generate its return value (result is an autoreleased NSString), but
wrong when -processString was a no-op (result is an unretained
reference to an NSString member). In the latter cases, result is
invalid at the NSLog.
You will admit that this is a bug in the implementation of
-processedString?
No. It is a bug in your code. If you want to use this result in the
fashion indicated, you must obviously retain it.
Why, then, is this not a bug:
NSString * result = [myNamedObject name];
[myNamedObject release];
NSLog(@"The name was %@" result); // MPW wants an access exception
here.
I don't *want* an access exception here. However an access exception
is perfectly valid here, the code snippet you show is *obviously* buggy.
You are not allowed to answer that I am _supposed to know_ how -name
is implemented.
EXACTLY, but this is precisely what you are doing when you access the
return value after its containing object is released. The only way to
NOT make this assumption is to retain the result yourself when you
release the container.
Senders of messages with NSObject * replies should be able to assume
that the replies have at least autorelease lifetimes.
This is simply false. As a simple counterexample, all the Foundation
collections do not conform to this "rule". The fact that you are using
the word *assume* should also tip you off that you are breaking your
own criterium that you set up just a line earlier.
Marcel
--
Marcel Weiher Metaobject Software Technologies
email@hidden www.metaobject.com
Metaprogramming for the Graphic Arts. HOM, IDEAs, MetaAd etc.
1d480c25f397c4786386135f8e8938e4
_______________________________________________
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.