Re: Accessor methods and (auto)release <Memory trail>
Re: Accessor methods and (auto)release <Memory trail>
- Subject: Re: Accessor methods and (auto)release <Memory trail>
- From: Greg Titus <email@hidden>
- Date: Wed, 31 Jul 2002 11:12:27 -0700
On Wednesday, July 31, 2002, at 10:27 AM, Ali Ozer wrote:
I am concerned about the more subtle case such as:
str = [myWindow title];
...
[myDocument saveDocument];
...
... use str ...
Assuming myDocument is the document in myWindow, a side effect of the
"saveDocument" call might be to release the title you got back in the
first line. A retain/autoreleased return from the first line would
eliminate this potential issue, which could be hard to debug.
It's also possible to build an example here of why Method 1 (setter
does autorelease) could be bad in this situation. Consider, if
saveDocument was implemented as:
pool = [[NSAutoreleasePool alloc] init];
...
if (isUntitled) {
...put up save panel, get new name...
[myWindow setRepresentedFilename:newDocumentFileName];
}
...
[pool release];
I am assuming here that setRepresentedFilename: ends up changing the
title of the window. In this case, due to the nested autorelease pool,
even if the setTitle: method did an autorelease, the value will still
be released when "str" is accessed in the outer function.
Hiya Ali,
You must not have had your coffee yet this morning or something. :-)
If the -title method was implemented to be { return [[title retain]
autorelease]; }, then the old title has been autoreleased into an
_outer_ autorelease pool (probably the default Cocoa pool around
handling each event). The fact that another pool is created and
destroyed inside -saveDocument is irrelevant. It won't touch the old
title, because that isn't the pool that was active when the old title
was autoreleased.
- Greg
_______________________________________________
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.