From: http://blog.csdn.net/diyagoanyhacker/archive/2011/05/30/6455244.aspx
Write Program It is not terrible to encounter bugs. Most problems are solved through simple log or Code The analysis is not difficult to find the cause. However, when the problem of exc_bad_access is encountered in objective-C programming, it is difficult to find the problem through simple and conventional means.
It is not terrible to encounter bugs when writing programs. Most of the problems are difficult to find the cause through simple log or code analysis. However, in objective-C
When the problem of exc_bad_access is encountered in programming, it is difficult to find the problem through simple and conventional means. This articleArticleTo introduce you to a common search
The root cause of the exc_bad_access problem.
First, let's talk about the exc_bad_access error. It can be said that the 90% error is caused by the release operation on a released object.
The objective-C code has three fatal problems: 1. Memory leakage; 2. Release of errors; 3. exc_bad_access error.
1, nsstring * s = [[nsstring alloc] initwithstring: @ "this is a test
String "]; An nsstring object is created, and the subsequent S = [s substringfromindex: [s
Rangeofstring: @ "A"]. Location]; after execution, the reference of the created object disappears, causing memory leakage.
2. Release error. [S release]; one of the reasons for this problem is a logic error, thinking that S is the nsstring we initially created
Object. The second reason is that the nsstring returned from the substringfromindex :( nsuinteger I) Method
Object, which is marked as autorelease by the substringfromindex method.
. If we forcibly release it, it will cause exc_bad_access problems.
3, exc_bad_access. Because the nsstring object to which s points is marked as autorelease
Records already exist in the NSAID utoreleasepool. However, since we released this object in the previous error, when [pool drain]
Now, the nyothoreleasepool calls the release method again for the s object it records.
If it has been released and no longer exists, it directly causes the exc_bad_access problem.
View More console Information
Workspace-> excuteables-> double-click a file under its group-> arguments to set running parameters
1: add the nszombieenabled environment variable to the project runtime. When exc_bad_access occurs, the xcode console prints the Problem description.
2: Add mallocstacklogging to enable malloc records
Make the following settings:
Project-> edit active executable-> argument
Add the following four parameters:
Nsdebugenabled
Nszombieenabled
Mallocstacklogging
Mallocstackloggingnocompact