• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: How can logging a pointer value cause EXC_BAD_ACCESS?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: How can logging a pointer value cause EXC_BAD_ACCESS?


  • Subject: Re: How can logging a pointer value cause EXC_BAD_ACCESS?
  • From: "Stephen J. Butler" <email@hidden>
  • Date: Sun, 29 May 2011 13:38:20 -0500

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. 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@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
>
_______________________________________________

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

  • Follow-Ups:
    • Re: [super dealloc] when init fails. Was: Logging pointer EXC_BAD_ACCESS?
      • From: Jerry Krinock <email@hidden>
References: 
 >How can logging a pointer value cause EXC_BAD_ACCESS? (From: Jerry Krinock <email@hidden>)

  • Prev by Date: How can logging a pointer value cause EXC_BAD_ACCESS?
  • Next by Date: Re: [super dealloc] when init fails. Was: Logging pointer EXC_BAD_ACCESS?
  • Previous by thread: How can logging a pointer value cause EXC_BAD_ACCESS?
  • Next by thread: Re: [super dealloc] when init fails. Was: Logging pointer EXC_BAD_ACCESS?
  • Index(es):
    • Date
    • Thread