----- Original Message ----
> From: Jens Bauer <[EMAIL PROTECTED]>
> Subject: Re: Cleanup inside a failed init method
> 
> On Dec 6, 2008, at 22:36, Charles Steinman wrote:
> 
> > The system will only call dealloc if the object is released. Happily, the 
> object should be released anyway if you plan to return nil since otherwise 
> you'll leak a half-initialized object every time the method fails. So you 
> should 
> release self and return nil.
> 
> I'm not sure about that. Take a look at this example:
> 
> // init only modifies an already allocated buffer.
> // ObjC and the O/S does not know what you are going to name your 'init' 
> method:
> 
> - (id)initIthMyParameters:(int)aParameter
> {
>   self = [super init];
>   if(self) // only if super returns a valid buffer
>   {
>     parameter = aParameter;
>   }
>   return(self);
> }
> 
> ob = [[MyObject alloc] initWithMyParameters:3];
> 
> The result of [MyObject alloc] is passed directly to initWithMyParameters:.
> 
> If alloc fails, it sends NULL as object to init. [NULL init] returns NULL, no 
> matter which object it is, so does [NULL sniff_sniff].
> -However, if +alloc succeeds, you have a buffer, it's passed directly to 
> initWithMyParameters:.
> 
> You're not guaranteed that you will get the same buffer that [MyObject alloc] 
> returns, because -init* will be able to deallocate it and return a different 
> (maybe even larger) buffer instead, and perhaps in a different memory zone.
> 
> Who/what is supposed to deallocate the allocated buffer between the [MyObject 
> alloc] and [myObject init] ?
> (How would the 'system' know that the object is to be deallocated?)
> The only reason would be that the line...
> 
>   self=NULL;
> 
> ...should automatically deallocate the object, and I've never heard that it 
> did.

I don't know why you think the object would be dealloced between alloc and 
init. If the if(self) conditional evaluates to nil, that's because [super init] 
returned nil.

Cheers,
Chuck



      
_______________________________________________

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 [EMAIL PROTECTED]

Reply via email to