Mads Lee Jensen
Mads Lee Jensen

Reputation: 4658

iPhone nslog "EXC_BAD_ACCESS"

Im trying to use the NSLog, to print console messages. The problem is sometimes i receive a "EXC_BAD_ACCESS" error when calling it

-(void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
NSLog(@"Working test %d", toInterfaceOrientation);
NSLog(@"EXC_BAD_ACCESS %@", toInterfaceOrientation);
}

Here i simply want to see what the arguments passed into the function contain. The first NSLog works fine. The second causes an "EXC_BAD_ACCESS" and i dont understand why?.

Upvotes: 8

Views: 2349

Answers (6)

Wymann
Wymann

Reputation: 71

EXC_BAD_ACCESS means your code is pointing to inaccessible memory address.

Such as:

  1. Make a pointer point to an invalid memory address
  2. Write to read-only memory
  3. Jump to an instruction at an invalid memory address
  4. Access a released object's property(send message to released object)

Your second line code causes a crash because of "%@". iOS thinks that the pointer you sent to it is an object, it access the property of that object, which doesn't exist, then it throws the EXC_BAD_ACCESS.

reference: Apple's Investigating Memory Access Crashes

Upvotes: 1

Matthias Bauch
Matthias Bauch

Reputation: 90117

%@ only works with objects. And toInterfaceOrientation is not an object.

As you can see in the documentation for UIInterfaceOrientation it's just an enum.

Upvotes: 13

Sonu
Sonu

Reputation: 957

toInterfaceOrientation is a enum variable... so if you want to print log of it you have to use %d ...... . and %@ mostly used for objects ...

Use this Code :

NSLog(@"EXC_BAD_ACCESS :%d",toInterfaceOrientation);

Upvotes: 1

Ajay
Ajay

Reputation: 2078

http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/Strings/Articles/formatSpecifiers.html

%@ is for objects only.

UIInterfaceOrientation is an enum: http://developer.apple.com/library/ios/#documentation/uikit/reference/UIDevice_Class/Reference/UIDevice.html#//apple_ref/doc/c_ref/UIDeviceOrientationPortrait

When you use %@ it is basically calling:

[UIInterfaceOrientation descriptionWithLocale]

Obviously this will cause a EXC_BAD_ACCESS

Upvotes: 2

Yannick Loriot
Yannick Loriot

Reputation: 7136

The second NSLog crash because you try to print an integer as a NSObject (%@ instead of %d). UIInterfaceOrientation is a enum it doesn't work.

Upvotes: 6

Hector204
Hector204

Reputation: 716

EXC_BAD_ACCESS usually means you're trying to call an object that's been released from memory. Try turning on NSZombies in your environment variables to see where it's causing the problem

Answer in a similar question here: How to use NSzombie in xcode?

Upvotes: 2

Related Questions