Re: Memory Management
Re: Memory Management
- Subject: Re: Memory Management
- From: Marco Scheurer <email@hidden>
- Date: Tue, 29 Jul 2003 17:01:18 +0200
On Monday, July 28, 2003, at 09:50 PM, Fritz Anderson wrote:
On Monday, 28 July 2003, at 1:44 PM, Marcel Weiher wrote:
On Monday, Jul 28, 2003, at 01:20 Europe/London, Martin Haecker wrote:
...
- (NSString *)name
{
return [[_name retain] autorelease];
}
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 once ran into a string-processing method in
OmniFoundation which, in no-op cases, returned its internal storage.
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?
I share Marcel's views on this subject. We wrote an article which shows
why we think that this pattern is a bad thing. It will hopefully be
published soon.
This pattern is obviously an attempt to fix in -processedString a
problem that is caused by = . In other environments the assignment
would prevent result from being freed during the lifetime of the
variable. Since it cannot be done here the "solution" is too extend a
little bit the lifetime of objects so that it works a little bit more
like a garbage collected environment.
Now consider the following code:
- doSomething
{
result = [ofProcessObject processedString];
[ofProcessObject release];
[self doSomethingElse];
}
- doSomethingElse
{
NSLog (@"The result is %@", result);
}
Shouldn't I also be able to claim that I am entitled to assume that
result has not been disposed-of? Why not? And what about the following?
- doSomething
{
result = [ofProcessObject processedString];
[ofProcessObject release];
[self performSelector:@selector(doSomethingElse) withObject:nil
afterDelay:0.0];
}
- doSomethingElse
{
NSLog (@"The result is %@", result);
}
Obviously, the similarity to a garbage collection is limited. Will
everybody stop claiming that result should not have been released in
every case?
Marco Scheurer
Sen:te, Lausanne, Switzerland
http://www.sente.ch
_______________________________________________
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.