What is EXC_BAD_ACCESS? How to Debug it?

Tram Ho

It is possible that during the iOS programming process you will encounter a crash with the error EXC_BAD_ACCESS. This article will help you understand what EXC_BAD_ACCESS is and what causes it, as well as a few tips to solve it.

1. What is EXC_BAD_ACCESS?

Once you understand the underlying cause of EXC_BAD_ACCESS, you will better understand its confusing name.

  • In simple terms: Whenever you encounter EXC_BAD_ACCESS, it means that you are sending a message to a freed object. This is the most common, but there are other exceptions that will be covered later.
  • Real meaning: In a more complex sense, in C and Objective-C, you will probably know pointers. A pointer is like a variable that stores an object. The pointer points to the memory address and takes its value.

When a command block is no longer pointed to, it is no longer used for your application. When that happens, the Kernel sends an exception (EXC) stating that your application is not authorized to access that memory (BAD_ACCESS).

In short, getting an EXC_BAD_ACCESS error means that your application is pointing to a device that cannot send messages anymore. However in some cases EXC_BAD_ACCESS occurs due to a pointer error.

2. Debug EXC_BAD_ACCESS

Debugging EXC_BAD_ACCESS seems very difficult, after understanding what it is, you will probably find it a little less difficult than before.

Zombie.

The first way to say it is zombie, which has been very popular and has appeared in Xcode for over a decade now. The name Zombie sounds dangerous, but it will be helpful for debugging EXC_BAD_ACCESS.

In Xcode you can turn on Zombie objects, which means the released objects will be retained as Zombie. In other words, the freed objects are still retained for debugging. If you access a Zombie object your application will get an EXC_BAD_ACCESS error immediately. Why is this useful for you? The hard part of EXC_BAD_ACCESS is that you don’t know what your application is trying to access on the object that has been freed. By retaining the liberated objects, Xcode can show where the error occurred.

Activating Zombie in Xcode is very easy. Note this may be different for each version of Xcode you are using. Select the scheme in the upper right corner, select Edit Scheme Select Run and go to the Diagnostics tab and tick Enable Zombie Objects.

Now if you have Zombie turned on, when you get into EXC_BAD_ACCESS case, the console will report an error like the following

In the above error we can see that it comes from respondsToSelector that tried to access an object that was freed. This will help you find the source of the crash and investigate it.

Analyze.

If the Zombie doesn’t solve your problem, the root cause may be more complicated. In this case, you need to take a closer look at your code, which can be complicated and time consuming. To help you easily find errors in code, you can ask Xcode to analyze your code so you can find problematic areas of code. To request Xcode analysis, select Analyze in the Product tab on the menu bar or use the shortcut Shift-Command-B . It will take several minutes to execute, after completing the issue will be displayed in the left navigation bar. Issues will be highlighted.

When you click on it, Xcode will redirect you to that code to review.

If you do not find the cause of EXC_BAD_ACCESS then you need to carefully review all the problems that Xcode has found.

3. Summary.

EXC_BAD_ACCESS is a common problem for programmers, it involves public memory management. It is less likely to occur from when ARC appeared, but that doesn’t mean it disappeared. 😄

Reference: https://code.tutsplus.com/tutorials/what-is-exc_bad_access-and-how-to-debug-it–cms-24544
Share the news now

Source : Viblo