Re: [super dealloc] when init fails. Was: Logging pointer EXC_BAD_ACCESS?
Re: [super dealloc] when init fails. Was: Logging pointer EXC_BAD_ACCESS?
- Subject: Re: [super dealloc] when init fails. Was: Logging pointer EXC_BAD_ACCESS?
- From: Ken Thomases <email@hidden>
- Date: Sun, 29 May 2011 14:48:05 -0500
On May 29, 2011, at 2:20 PM, Jerry Krinock wrote:
> So, let's look at the subclass init method which invokes -dealloc:
>
> - (id)initWithDocUuid:(NSString*)docUuid {
> NSManagedObjectContext* moc ;
> moc = [[BkmxBasis sharedBasis] exidsMocForIdentifier:docUuid] ;
>
> self = [super initWithManagedObjectContext:moc
> entityName:constEntityNameStarxid] ;
> if (!self) {
> [super dealloc] ;
> }
>
> return self ;
> }
>
> It seems that either that recommendation is bad, or I misintepreted how to do it. What's wrong with that init method?
Well, personally, I don't like that recommendation, but far be it from me to argue with one of the runtime engineers. However, you've definitely misinterpreted it.
If super's init method fails, then there's nothing for you to do. First, self will be nil, as you've discovered. Second, super is responsible for having cleaned up.
The recommendation was to invoke [super dealloc] if the super's init succeeded, but you subsequently can't complete your initialization. In other words, if self is _not_ nil, and you proceed to try to allocate stuff or open a file or whatever, but you fail. In that case, you are responsible for cleaning up and returning nil. The recommendation regards _how_ you clean up.
Regards,
Ken
_______________________________________________
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