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: Jerry Krinock <email@hidden>
- Date: Sun, 29 May 2011 12:20:22 -0700
On 2011 May 29, at 11:38, Stephen J. Butler wrote:
> On Sun, May 29, 2011 at 1:30 PM, Jerry Krinock <email@hidden> wrote:
>> I'm really losing it; or maybe I never understood to begin with. How can this code crash?
>>
>> - (void)dealloc
>> {
>> NSLog(@"0988 %p %s", self, __PRETTY_FUNCTION__) ;
>> NSLog(@"1250 ") ;
>> CRASH-> int myPointer = (int)m_managedObjectContext ;
>> NSLog(@"1335 myPointer = %d", myPointer) ;
>> ...
>> }
>>
>> stderr output:
>>
>> 0988 0x0 -[SSYMojo dealloc]
>> 1250
>> Program received signal: “EXC_BAD_ACCESS”
>
> This is HIGHLY suspicious. Look at your first line, dealloc is somehow
> getting sent to a nil self … you never call dealloc yourself in code unless
> it's [super dealloc].
Thank you, Stephen. Indeed, that's how I got there. A subclass' -init failed and sent a [super dealloc] as recommended here:
http://lists.apple.com/archives/Objc-language/2008/Sep/msg00133.html
> Number two, Obj-C short circuits messages to
> "nil" objects: they aren't even called.
>
> The crash happens because m_managedObjectContext is an instance
> variable (I assume), and there's an implied dereference of self (that
> is, it's really "self->m_managedObjectContext"). Since self is nil,
> you're dereferencing NULL and you get a segfault.
> So the crash is the logical conclusion of dealloc being called on a
> nil object.
Ah, I get it now. It's the access to the pointer m_managedObjectContext itself that's the problem.
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?
Jerry
_______________________________________________
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