How to debug EXC_BAD_ACCESS error

Posted: July 2, 2012 in iOS Development, Quick Tips
Tags: , , , ,

XCode makes the life of iOS developers easier by providing the ability to debug code using break points, backtrace error logs and pin point the cause of errors when they occur. You can also let the compiler speak out the log message. If you haven’t played with it just go ahead and edit one of your break points to add an action when an error occurs.

But every now and then you will notice this beast “EXC_BAD_ACCESS”. As an iOS newbie you will hate this error and often I have seen pro devs struggle to find out the exact cause of this error. As this is one such error that will mostly throw you to your main.m file and you can’t find what exactly is causing this crash.

One thing I have learnt over last few years and that has helped me to debug an error faster is – “An error is what the error says”.

So before posting your ugly error message on stackoverflow or before even hitting google, stop for a while and look carefully what the error message tells. So let’s say I get this error on my tableViewController, or scrollViewController then before getting into what is wrong in my so and so view controllers, I would probably look first into what does “EXC_BAD_ACCESS” mean if I don’t know about it. Apple’s error messages are verbose and self explanatory. Hence, as one can clearly make out from the description “EXC_BAD_ACESS” means you are doing bad access or in other words you are accessing something that you shouldn’t.

Talking more in Obj-C terms – a message was sent to an object that doesn’t exist or that doesn’t understads the message. Hence, bad access.

You will get EXC_BAD_ACCESS error mostly in the following scenarios:

  1. You are trying to access an object that is not initialized.
  2. You are trying to access an object that no longer exists. Either it’s being released or it’s nil. In ARC mode, make sure you take ownership of the object that you want to use.
  3. You are passing an message to an object that the object doesn’t understand. It can also happen for bad typecast. Like the lines below where I am trying to access an int with %@ in stead of %d.
int myAwesomeInt = 9;
NSLog(@"%@", myAwesomeInt);

How to debug:

Identify what you did that caused the crash. Did it crash while view of a particular view controller didLoad or in a delegate method or on a particular action. That will often help to find the object that is casuing the error.

  • Most of the time “NSZombies” can help to identify the dead object. You can enable NSZombies by editing your scheme Product -> Edit Scheme -> Diagnostics.
  • If you still don’t find the root cause then always go backwards from child view controller to parent view controller to see what object needs to be retained or what message needs to be passed properly.
  • Look into Static Analyzer and Instruments for advanced debugging. 
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s