• 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: Proper retain/release etiquette
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Proper retain/release etiquette


  • Subject: Re: Proper retain/release etiquette
  • From: Ondra Cada <email@hidden>
  • Date: Tue, 23 Jul 2002 20:34:47 +0200

On Tuesday, July 23, 2002, at 05:09 , Roarke Lynch wrote:

Well, since it is a very very common bug, I would explicitly add that the pattern

// *WRONG*
id foo=[[Xyz alloc] init];
// other code here
[foo release];

is bound to leak as soon as the "other code" happens to raise. Therefore,
the proper pattern is

// *RIGHT*
id foo=[[[Xyz alloc] init] autorelease]; // or just [Xyz xyz], if available
// other code here

or, if you have very hard reasons not to autorelease, you need to use NS_DURING/NS_HANDLER/NS_ENDHANDLER.

What would make this leak? I was under the impression that autorelease was basically a delayed release call.

Right. And, in the *RIGHT* case, it was already scheduled before exception occured. In the *WRONG* case though the release is never reached (in the case of an exception).

Incidentally, it might be worth mentioning that the NSAutorelesePool is smart enough to keep track of "orphaned" pools as well, so that *nothing leaks* even in scenarios like this:

NSAutorelesePool *pool=[[NSAutorelesePool alloc] init];
id foo=[[[Xyz alloc] init] autorelease];
[NSException raise:...];
[pool release]; // never reached: DOES NOT HARM IN THIS SPECIAL CASE!

The foo object *will* be released when the parent pool releases.
---
Ondra Cada
OCSoftware: email@hidden http://www.ocs.cz
private email@hidden http://www.ocs.cz/oc
_______________________________________________
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.

References: 
 >Re: Proper retain/release etiquette (From: Roarke Lynch <email@hidden>)

  • Prev by Date: Re: Document App: IB connections 101
  • Next by Date: Re: Document App: IB connections 101
  • Previous by thread: Re: Proper retain/release etiquette
  • Next by thread: Re: Proper retain/release etiquette
  • Index(es):
    • Date
    • Thread