• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Accessor methods and (auto)release <Memory trail>
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Accessor methods and (auto)release <Memory trail>


  • Subject: Re: Accessor methods and (auto)release <Memory trail>
  • From: Ali Ozer <email@hidden>
  • Date: Wed, 31 Jul 2002 10:27:11 -0700

So, to flesh out your example, you are concerned about this kind of code:

{
str = [someObj title];
if (someCondition)
[someObj setTitle:@"foo"];
NSLog (@"The string is %@", str];
}

If so, it seems to me that the proper technique is to substitute

str = [[[someObj title] retain] autorelease];

for the first line of code.

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.

Method 2, where you do retain/autorelease in the getter, ends up being safer. In cases where performance isn't critical ([window title] is not likely to be called thousands of times a second), I believe it's a good idea to go the safer route to reduce surprise for clients.

Of course there are exceptions, such as the Foundation collection classes as Ondra pointed out in an email to me. There, the collections are just that --- low-level containers that hold your objects --- and we don't want them autoreleasing or otherwise changing the lifetimes of your objects, and of course performance is a major consideration. So there, setObject:forKey:, removeObject:, etc are all very explicit, without any autoreleasing.

Ali
_______________________________________________
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.

  • Follow-Ups:
    • Re: Accessor methods and (auto)release <Memory trail>
      • From: Greg Titus <email@hidden>
References: 
 >RE: Accessor methods and (auto)release <Memory trail> (From: "Jonathan E. Jackel" <email@hidden>)

  • Prev by Date: Re: apple help
  • Next by Date: Re: Treating several controls as an array (...or something)
  • Previous by thread: RE: Accessor methods and (auto)release <Memory trail>
  • Next by thread: Re: Accessor methods and (auto)release <Memory trail>
  • Index(es):
    • Date
    • Thread