Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to debug EXC_BAD_ACCESS in iPhone app when I can't determine the cause?

Help, I've been hacking at this for weeks now! I have this app I am developing in the Simulator, and I have done a lot of the UI stuff, it is just getting to the interesting part with data. It started immediately upon launch, about 90% of the time I run it, it will raise EXC_BAD_ACCESS exception.

I have commented out all of my release messages, and even added some retain messages to be sure it is not something that is over released. Weird thing is, sometimes something I do in the code will make it work, then it works until I make another code change. Then I comment out the new code I added, and it still crashes.

I have read through probably a dozen articles on the web on this, tried the things they suggest. I have set breakpoints, and still can't figure out where it is. When I click up the call stack in the debugger, the only place that shows source code is the bottom level which is main.

The debugger has this stack, but that fluctuates a bit each time it crashes. The inconsistent nature tells me there is some memory it is autoreleasing, but I have no idea how to find out what.

0 objc_msgSend
1 ??
2 _CFAutoReleasePoolPop
3 -[NSAutoReleasePool release]
4 _UIApplicationHandleEvent
5 PurpleEventCallback
6 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION__
7 __CFRunLoopDoSource1
8 __CFRunLoopRun
9 CFRunLoopRunSpecific
10 CFRunLoopRunInMode
11 -[UIApplication _run]
12 UIApplicationMain
13 main

A few more facts:

  • I set NSZombieEnabled, that didn't make a difference in the console output
  • I ran Instruments using the Leaks profile, it didn't show any leaks
like image 414
Jay Imerman Avatar asked Apr 13 '11 23:04

Jay Imerman


1 Answers

you will want to enable zombie objects in your code, and Checking Autoreleased objects, and perhaps enabling debugging will help.

I added three Environment Variables.

  • NSZombieEnabled
  • NSAutoreleaseFreedObjectCheckEnabled
  • NSDebugEnabled

all of these are set to YES

here is a link with the path I took.

http://www.codza.com/how-to-debug-exc_bad_access-on-iphone

if you are using XCode 4 then you will add these in the Arguments section of the Edit Schemes popover.

Another thing to note is, you should only release or autorelease objects that you are retaining. You maintain a retain on the following objects.

  • Any object you alloc [NSObject alloc]
  • Any object obtained using the static new command [NSObject new]
  • Any object you explicitly retain [myObject retain]
  • Any copy of an object [myObject copy]
  • Any property with the retain or copy attribute @property (retain) NSString *myProperty;

if you send an autorelease to any object other than these, you could randomly end up with this and other errors.

commonly I release objects and then set them to nil, that way if i release them later, I wont have any issue because if you autorelease nil, you get nil.

NSObject *myObject = [incomingObject retain];
// Do something with the object.
[myObject autorelease];
[myObject autorelease]; // This line will end in an error down the line when the object is released past 0, or when the release pool is drained.
myObject = nil;
[myObject release]; // This line will do nothing. no error, no effect.
like image 191
The Lazy Coder Avatar answered Sep 20 '22 14:09

The Lazy Coder