15

Is there a way to prevent an EXC_BAD_ACCESS from crashing an app, like with @try..@catch you can handle an exception gracefully.

Update:

The code crashes when it attempts to dereference an invalid pointer. This is a third party library and it interfaces with external hardware so I can't debug locally. I am trying to prevent it from crashing and output data to a debugging console on my app.

Boon
  • 40,656
  • 60
  • 209
  • 315

3 Answers3

14

In ObjC, try/catch do not handle exceptions particularly gracefully. You will still leak memory and leave the system in an undefined state. With rare exception, the expectation is that you are simply catching so you can log some things before crashing. And in general, you should not use @catch anywhere but at the top level of your program for this purpose. There are some extraordinary situations where limited use of exceptions may be appropriate, but they are rare in ObjC. See the Exception Programming Guide for some more information. See especially the following from the ObjC ARC documentation:

The standard Cocoa convention is that exceptions signal programmer error and are not intended to be recovered from. Making code exceptions-safe by default would impose severe runtime and code size penalties on code that typically does not actually care about exceptions safety. Therefore, ARC-generated code leaks by default on exceptions, which is just fine if the process is going to be immediately terminated anyway. Programs which do care about recovering from exceptions should enable the option [-fobjc-arc-exceptions, which imposes speed and memory penalties on your program].

The same is true of EXC_BAD_ACCESS. You can catch it with a signal handler for the purpose of recording some information and then finishing your crash. For a good tool for doing this see PLCrashReporter. It is very difficult to write such a handler correctly, so I strongly recommend using an existing framework. You can easily get into deadlocks that drain the user's battery if you catch EXC_BAD_ACCESS incorrectly.

Rob Napier
  • 286,113
  • 34
  • 456
  • 610
12

You get EXC_BAD_ACCESS often because you sent a message to a released object. Then you can examine the NSZombie. What is an NSZombie? You can see : this. It will catch the EXC_BAD_ACCESS because of sent a message to the released object.

You can set NSZombie like this : Check the Enable Zombie Objects enter image description here

And you can also get EXC_BAD_ACCESS because of the memory warnings level is too high , or your memory is too high , so the apple will shut your app down. This EXC_BAD_ACCESS is too hard to prevent . I think the only way is to manage your memory as low as you can , sometimes you can see the log where is receive memory warning , when the level is high, it may getEXC_BAD_ACCESS

Community
  • 1
  • 1
Guo Luchuan
  • 4,729
  • 25
  • 33
8

You can rewrite your code to not have these errors. Try not to reference any null pointers and keep references to any object that you want to have access to.

Jsdodgers
  • 5,253
  • 2
  • 20
  • 36
  • 1
    This! Some errors, you really _don't_ want to try and continue :-) – paxdiablo Mar 20 '13 at 02:02
  • 1
    +1 With just a little care and defense in programming, rigorous consistency (particularly using accessors rather than ivars), and using "treat warnings as errors," I find that these kinds of crashes become very rare. – Rob Napier Mar 20 '13 at 02:16
  • 2
    Thanks for the advice but it's not my code. It's a third party library code and I am trying to debug remotely. I can't do debugging with Xcode as it interfaces with external hardware. I asked this question for a reason. – Boon Mar 20 '13 at 02:44
  • As I noted in my answer, PLCrashReporter will allow you to log information about the crash before terminating. You can also use `ulimit` to allow the process to write a core file, which you can then investigate with gdb. A bit more powerfully, there is Google's Breakpad, but I find it a bit challenging to use well. None of these things will, or should, prevent the crash. They will just provide more information about the crash. http://stackoverflow.com/questions/2080918/where-are-core-dumps-written-to-in-mac-os-x – Rob Napier Mar 20 '13 at 18:28
  • 2
    This is really a non-answer. I'm writing a unit test and I want to assert that the code never changes in a way which will allow EXC_BAD_ACCESS... – Clayton Rabenda Apr 02 '15 at 21:16