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 (Cocoa-dev@lists.apple.com)

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:
http://lists.apple.com/mailman/options/cocoa-dev/jsbache%40adobe.com

This email sent to jsba...@adobe.com

_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

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:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Reply via email to