Detailed explanation of the essence of iOS EXC_BAD_ACCESS and zombie mode debugging

Original link

Simple understanding of EXC_BAD_ACCESS

When you encounter a crash caused by EXC_BAD_ACCESS, it means that you send a message to an object that has been released. This is the most common situation.

The essence of EXC_BAD_ACCESS

In C and Objective-C, you have been dealing with pointers. A pointer is nothing more than a variable that stores the memory address of another variable. When you send a message to an object, the pointer to that object will be referenced. This means that you get the memory address pointed to by the pointer and access the value in that storage area.

When the memory area is no longer mapped to your application, or in other words, the memory area is not used when you think it is used, the memory area is inaccessible. At this time, the kernel will throw an exception (EXC), indicating that your application cannot access the memory area (BAD ACCESS).

In short, when you encounter EXC_BAD_ACCESS, it means the memory block you are trying to send a message to, but the memory block cannot execute the message. However, in some cases, EXC_BAD_ACCESS is caused by a damaged pointer. Whenever your application attempts to reference a corrupted pointer, an exception will be thrown by the kernel.

Three points of EXC_BAD_ACCESS debugging instructions

1. Debugging EXC_BAD_ACCESS can be very tricky and frustrating.
2. What you need to know is that your application is not necessarily inaccessible to the memory area at the moment of crash. This is why it is often difficult to debug EXC_BAD_ACCESS.
3. The same is true for damaged pointers. When your pointer is damaged, your application will not crash. At the same time, if you pass a damaged pointer back and forth in the application, it will not crash. When an application tries to reference a damaged pointer, it crashes.

EXC_BAD_ACCESS debugging-zombie debugging mode

In Xcode, you can enable zombie objects, which means that the released objects will be retained as zombies. In other words, keeping the released objects is for debugging. No magic is involved here. If you send a message to a zombie object, your application will crash due to EXC_BAD_ACCESS.

Is this any good? What makes EXC_BAD_ACCESS difficult to debug is that you don't know which object your application is trying to access. Zombie objects solve this problem in many cases. By retaining the released objects, Xcode can tell you which object you are trying to access, which makes finding the cause of the problem much easier.

Specific operation of zombie debugging mode

Enabling zombie objects in Xcode is very easy. Click Edit Scheme in the upper left corner and select Edit Scheme. Select Run on the left and open the Diagnostics option at the top. To enable zombie objects, check the Zombie Objects box.

Zombie debugging mode

If you encounter EXC_BAD_ACCESS now, output it in the Xcode console to tell you where to find the problem. Take a look at the example output below.

  -[CLTextView textInputView]: message sent to deallocated instance 0x7acb0200

In the above example, Xcode tells us that the message of textInputView: is sent to a zombie object. However, the zombie object is no longer an instance of the CLTextView class. The memory area previously allocated to the CLTextView instance is no longer mapped to your application. This provides a good suggestion for you to understand the root cause of the problem. Unfortunately, the zombie object will not be able to save the EXC_BAD_ACCESS record for each crash of your day. Since the zombie object does not have these methods, you can take other methods for proper analysis.
The reason for the zombie object in the above example: When writing the UITextView category, a notification named UITextViewTextDidChangeNotification was added, which was released in the following code after use, causing an error.

- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self
                                                    name:UITextViewTextDidChangeNotification
                                                  object:self];
}

Xcode analyzes the project (if the zombie object cannot solve your problem)

To use Xcode to analyze your project, select Analyze from Xcode's Product menu or press Shift-Command-B. Xcode will take a moment, but when it is completed you will see a list of issues in the Issue Navigator on the left. Problems found by Analyze are highlighted in blue.

Xcode analysis

When you click on a question, Xcode will point to the question code block, which is exactly what you need to pay attention to. Note that Xcode is only a recommendation. In some cases, this is possible, and the problem is irrelevant and not fixed. If you can't find the error that caused the EXC_BAD_ACCESS, you need to carefully examine the Xcode project and analyze every problem found in it.

Guess you like

Origin blog.csdn.net/Draven__/article/details/90575578