Re: When init returns nil does it cause a leak
Re: When init returns nil does it cause a leak
- Subject: Re: When init returns nil does it cause a leak
- From: Jesper Storm Bache <email@hidden>
- Date: Tue, 19 May 2009 09:32:33 -0700
Apple's take on this is that when an initializer fails, then it should
call release on itself and return nil.
See: http://developer.apple.com/documentation/Cocoa/Conceptual/ObjectiveC/Articles/ocAllocInit.html#/
/apple_ref/doc/uid/TP30001163-CH22-SW13
This means that if super returns nil, then your memory should already
be deallocated.
I personally disagree with the Apple recommendation, and I vote for
calling [super dealloc] when initialization fails - because this will
invoke dealloc only in the base classes where init succeeded.
In any case, when nil is returned, you should expect that your storage
may already have been deleted.
Jesper Storm Bache
On May 19, 2009, at 9:24 AM, Reza Farhad wrote:
Hi all
we have an object that gets initialized like most other objects
-(id)init
{
self = [ super init ];
if ( self ){
...do something;
}
return self;
}
if [ super init ] returns nil does this cause a leak, as the memory
has already been created by calling alloc when trying to create the
object
AnObject *object = [[ AnObject alloc ] init ];
I am sure the answer is simple but this suddenly stumbled me.
Thanks
Reza
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden