Note: errors and exceptions are inevitable during code writing. This shows the importance of debugging. The following is a summary, I usually use some debugging skills and summarize the skills used by other netizens on the network. (New debugging skills will be available in the future, and I will update them together)
1. Use a zombie variable (NSZombieEnabled)
2. Override the respondsToSelector method of the object.
It is difficult to find the cause of the EXC_BAD_ACCESS bug During iphone development.
First, let's talk about the EXC_BAD_ACCESS error. It can be said that the 90% error is caused by performing the release operation on a released object, or operating an object in the sequence modified in the loop code. Although the NSZombieEnabled variable can help you locate the problem, sometimes, even if you still cannot locate the problem by setting the NSZombieEnabled variable, you can try to override the respondsToSelector method of the object, the last object accessed before EXEC_BAD_ACCESS is displayed. The specific steps are as follows:
A. Add-D _ FOR_DEBUG _ to each class and other c flags (remember to add this flag only under Debug Configuration ). In this way, when your program crashes, the Xcode console will accurately record the method of the last running object. In the implementation file (. m,. mm), add the following code:
[Cpp]
-(BOOL) respondsToSelector: (SEL) aSelector {
Printf ("SELECTOR: % s \ n", [NSStringFromSelector (aSelector) UTF8String]);
Return [super respondsToSelector: aSelector];
}
B. Add-D _ FOR_DEBUG _ to other c flags (remember to add this flag only under Debug Configuration ). In this way, when your program crashes, the Xcode console will accurately record the method of the last running object.
3. Enable XCode to provide sufficient information.
Find Arguments in Edit-> Scheme and add it to Environment Variables.
Set the following two values to YES.
NSAID utoreleasefreedobjectcheckenabled
NSDebugEnabled
This method is very useful. We recommend that you add this setting when creating a project.
4. Set a global exception breakpoint
When the program throws an exception, it is often necessary to locate the exception
Enter the shortcut key cmd + 6 to enter the BreakPoint window, click the "+" button in the lower left corner, and select the "Add Exception BreakPoint" option in the pop-up box, as shown in:
Use the default settings and click "done" to set the global exception breakpoint.