On Sun, May 29, 2011 at 1:30 PM, Jerry Krinock <[email protected]> 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. I have no idea how you'd end up in such a situation. For one, you never call dealloc yourself in code unless it's [super dealloc]. 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. But I have zero ideas how you even got to that situation in the first place. > The line marked CRASH-> is highlighted in blue by Xcode/gdb. How can this > crash? I wanted to log the pointer value (and it *is* an 'int' since this is > 32-bit). To emphasize this, I assigned it to an int, and it still crashes. > I thought that EXC_BAD_ACCESS can only occur if you *access* memory. > > Of course, m_managedObjectContext is declared as a pointer to an object in > the @interface, > > NSManagedObjectContext* m_managedObjectContext ; > > I've never seen this happen before. I know the format specifier %@ can evoke > a crash because it sends a -description message. But not %p or %x or %d. > > Just to prove that gravity still pulls downward, I tried this: > > NSManagedObjectContext* x = (void*)3 ; > // Certainly 0x3 is not in my memory space! > NSLog(@"Hello") ; > NSLog(@"x=%d", (int)x) ; > int y = (int)x ; > NSLog(@"World") ; > NSLog(@"y=%d", y) ; > > stderr output: > > Hello > x=3 > World > y=3 > > No crash, as expected! > > What might be the difference between this and my -dealloc method? > > Jerry > > _______________________________________________ > > Cocoa-dev mailing list ([email protected]) > > 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/stephen.butler%40gmail.com > > This email sent to [email protected] > _______________________________________________ Cocoa-dev mailing list ([email protected]) 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]
