Re: Proper retain/release etiquette
Re: Proper retain/release etiquette
- Subject: Re: Proper retain/release etiquette
- From: Chris Bracken <email@hidden>
- Date: Mon, 22 Jul 2002 22:15:39 -0700
My two cents;
10. It is bad practice to do: id bar = [Foo alloc]; [bar
init]; because init
might hand back a different object than the original alloc did.
This is programmatically similar to nesting the two i.e id
bar = [[Foo alloc] init]. So i think it makes no difference.
Well, it *could* make a difference...
id bar = [[Foo alloc] init];
If init returns nil due to some catastrophic failure
initializing, and the object releases itself, you get the good
situation where bar == nil with the above line of code you can
check for this and -- if I remember correctly -- messages sent
to nil are ignored.
On the other hand, if you do this
id bar = [Foo alloc];
[bar init];
you get the possibility that if initialization fails and returns
nil, the object releases itself, but your pointer (bar) isn't
necessarily set to nil. You don't know if init failed until you
try to send a message to bar. Uh-oh.
Of course, this is a moot point because there isn't any really
good reason to split this oh-so-common line of code in two in
the first place. Save those extra keystrokes and clump it
together into one big happy operation.
Chris
_______________________________________________
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.