• 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: Which class methods return autoreleased objects?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Which class methods return autoreleased objects?


  • Subject: Re: Which class methods return autoreleased objects?
  • From: "M. Uli Kusterer" <email@hidden>
  • Date: Tue, 7 Dec 2004 05:00:49 +0100

At 18:59 Uhr -0500 06.12.2004, Gwynne wrote:
That's the long answer :). The short answer is that if a method doesn't have "init" or "copy" in its name, it returns an object you shouldn't release unless you retain it first.

"alloc"/"init", "new" or "copy", with the NIB loading functions being a special case where the owner needs to release all the top-level objects in the file (NSDocument and some other standard classes that own a NIB do that for you, though). Also note that alloc/init are mentioned as a couple, because alloc actually creates the object, but init then assumes ownership of the object before it later hands on the object and transfers ownership to you.


So, if you're writing your own init method and need to error-exit, be sure to release self and return NIL, or you'll have a leak.

This USUALLY translates to autoreleased, but there are some instances in which it doesn't. [NSFileManager defaultManager] comes to mind. That object is (probably) not autoreleased, but you shouldn't release it yourself either. On the other hand, it's probably a safe bet that [NSString stringWithFormat:] returns an autoreleased string.

Another case is NSArray's objectAtIndex: The array retains the object, and doesn't return an autoreleased copy, which means the following code will crash:


NSMutableAttributedString obj = [theArray objectAtIndex: 0];
[theArray release]; /* obj was owned by the Array, and is released now. */
[obj setString: @"foo"]; /* bang! BAD_ACCESS */


You should treat both cases the same way: unless you put your own retain on the object, it is only valid in the autorelease scope in which you received it. Autorelease scope is never larger than function scope, though it can be smaller if you create your own autorelease pool inside a function. Here are some examples:

Actually, it *can* be larger than the current function/method, but you shouldn't rely on it. The default autorelease pool, for example, is created after the first user action (e.g. after the user chooses a menu item) and is released when your code returns control to Cocoa (and thus to the user).


This is usually around your action method, so that method's scope is the scope of autoreleased objects as far as you can tell. However, since e.g. loops are free to create their own nested autorelease pools, you'd be introducing a dangerous source of hard-to-track errors if you rely on that, because your methods might bomb when called from such a loop. Don't do it.
--
Cheers,
M. Uli Kusterer
------------------------------------------------------------
"The Witnesses of TeachText are everywhere..."
http://www.zathras.de
_______________________________________________
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
  • Follow-Ups:
    • Re: Which class methods return autoreleased objects?
      • From: Charlton Wilbur <email@hidden>
References: 
 >Which class methods return autoreleased objects? (From: Jeremy French <email@hidden>)
 >Re: Which class methods return autoreleased objects? (From: Ricky Sharp <email@hidden>)
 >Re: Which class methods return autoreleased objects? (From: Gwynne <email@hidden>)

  • Prev by Date: Re: Why is viewDidMoveToWindow called multiple times?
  • Next by Date: Re: Which class methods return autoreleased objects?
  • Previous by thread: Re: Which class methods return autoreleased objects?
  • Next by thread: Re: Which class methods return autoreleased objects?
  • Index(es):
    • Date
    • Thread