Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is EXC_BAD_ACCESS so unhelpful?

Tags:

objective-c

First let me say I come from a background in Flash/AS3, which I realize is not as strict about most things as iPhone/Objective-C.

I suspect my question actually applies to AS3 as well, but let me ask it as pertaining to Obj-c. Why is the error EXC_BAD_ACCESS, and others like it, so unhelpful? I realize that it normally means mismanagement of memory somewhere, but why can't it tell you more about the problem. For instance why doesn't it say "EXC_BAD_ACCESS, you tried to pass pointer suchAndSuch on line 123, however you're an idiot, because you released it on line 69 so it's not available anymore"?

I realize I can use the debugger to get more clues about where my error occurred, but many times this is only marginally helpful. For instance sometimes none of the messages in the stack/thread/whatever are even my code. Other times it is my code but on the top of the stack will be a message that has 4+ parameters, ok thanks debugger you narrowed it down to 4 possible pointers by why can't you just tell me which one!?

I'm guessing there's just some fundamental explanation that I missed because of the background I came from, not needing to worry about memory and such. Although there is an error that can happen a lot in AS3 development that is equally mysterious and along the same lines. "Error #1009: Cannot access a property or method of a null object reference" which almost always means a variable you were expecting to be holding something is actually null. Why doesn't it tell me WHICH variable?!

like image 967
Dustin Avatar asked Jan 13 '10 21:01

Dustin


1 Answers

Because Objective-C's runtime is quite small and simple, on purpose. You are practically coding a native app that's sitting right on the metal. The app has no idea why you got an EXC_BAD_ACCESS, all it knows is you tried to do something and the OS said no. EXC_BAD_ACCESS means you are attempting to access memory space that the OS did not give to you.

In Flash, Java, .NET, etc there is a hefty, powerful runtime sitting there running your app for you. It has a lot more context, and knows a lot more about what is going on. So a benefit of an environment like this is much clearer error messages, usually with a full stack trace.

Xcode can help you, look into zombifying your objects. Instead of deallocating them, they will become zombies and scream bloody murder if you try to use them again. Which helps pin down the cause.

You may also want to look at the Technical Q&A or the Debugging Magic

like image 158
Matt Greer Avatar answered Sep 22 '22 14:09

Matt Greer