Re: Proper retain/release etiquette
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.