Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to catch or handle EXC_BAD_ACCESS?

Tags:

As far as I understand, EXC_BAD_ACCESS happens when you try to access bad memory (feel free to correct me if I'm wrong)?

Is there a way to kind of catch it like in a try-catch in Java to prevent total app failure?

like image 817
corgichu Avatar asked Apr 24 '13 20:04

corgichu


People also ask

What causes Exc_bad_access?

EXC_BAD_ACCESS is an exception raised as a result of accessing bad memory. We're constantly working with pointers to memory in Swift that link to a specific memory address. An application will crash whenever we try to access a pointer that is invalid or no longer exists.

What does thread 1 Exc_bad_access mean?

EXC_BAD_ACCESS means that message was sent to a point in the memory where there's no instance of a class to execute it. Thus “bad access”. You will get EXC_BAD_ACCESS in 3 cases: An object is not initialized.

How to debug EXC_ BAD_ ACCESS iOS?

To debug an EXC_BAD_ACCESS, you can generally find out the where the dangling pointer is by enabling zombie objects. Choose edit scheme, then Diagnostics tab in the Run section, then click the 'Zombie Objects' option. Another cause for EXC_BAD_ACCESS can be infinite recursion, which can be found by adding some logging.


1 Answers

Nope; EXC_BAD_ACCESS means things have gone wildly off the rails. Your program is trying to access a memory address that is invalid. I.e. memory has been corrupted and there is no predictable recovery.

It may be a memory management issue. If you can reproduce the issue, turn on NSZombies and see what happens. Or post the backtrace here.

Note that try-catch style exceptions are non-recoverable in iOS/Cocoa, too. Exceptions are not to be used for recoverable error handling. That is what NSError is for.

like image 95
bbum Avatar answered Sep 24 '22 15:09

bbum