How can logging a pointer value cause EXC_BAD_ACCESS?
How can logging a pointer value cause EXC_BAD_ACCESS?
- Subject: How can logging a pointer value cause EXC_BAD_ACCESS?
- From: Jerry Krinock <email@hidden>
- Date: Sun, 29 May 2011 11:30:41 -0700
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”
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@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