We do iOS program development often with the encounter exc_bad_access error caused Crash, when this error occurs generally Xcode will not give us too much information to locate the source of the error, just left on the application Delegate like thread 1:program receiv Ed signal: "Exc_bad_access" to make the problem impossible to find.
For example, when you send a message to an object that has already been disposed of, exc_bad_access, such as the release object and then release,release those Autorelease objects, will also report such a mistake. By default, Xcode does not give you a specific line of code, you should not use the freed object, or the release is wrong.
For example, a code like this in a Uiviewcontroller subclass:
[CPP]View Plaincopyprint? < param name= "wmode" value= "Transparent" >
- Static Nsmutablearray*array;
- -(void) Viewdidload
- {
- [Superviewdidload];
- array= [[Nsmutablearray alloc]initwithcapacity:5];
- [Array release]; //Release the array
- }
- -(void) Viewwillappear: (BOOL) animated{
- [Array addobject:@"Hello"]; //Use the freed array
- }
The above code will have a exc_bad_access error, but when I do, Xcode is positioned in the application:didfinishlaunchingwithoptions of my appdelegate: a line on the method, If you have a lot of code, it's hard to find a specific problem, but with experience.
However, the nszombieenabled environment variable can help us, that is, when the nszombieenabled environment variable is set, an object is converted to _nszombie when destroyed, and after the nszombieenabled is set, When you send a message to an object that has already been disposed of, the object does not crash or produce an incomprehensible behavior, but instead emits an error message and disappears in a predictable way that produces a debug breakpoint. So we can find specific or presumably which object has been wrongly released.
After the nszombieenabled is set for Xcode, Xcode explicitly locates the row [array addobject:@ "Hello"], and then the console reports an error message:
-[__nsarray Addobject:]:message sent to deallocated instance 0x6557370
How to set nszombieenabled, under the Xcode3 and XCODE4 settings are not the same, Xcode4 settings is very simple.
Xcode3 under nszombieenabled The Setup method is as follows:
1. Locate executables in the groups& files column on the left of Xcode, double-click one of them, or right-get Info;
2. Switch to arguments
3. Here is a total of two boxes, in the following variables to is set in theenvironment: Dot + number Add an item, name fill Nszombieenabled,value fill in the Yes, to ensure that the front hook is selected.
Xcode4 under Settings nszombieenabled method of:
You can click the Xcode4 menu Product, Edit scheme-> Arguments, and then click "Plus" to add the nszombieenabled parameter to the Environment Variables window, followed by the number Write "YES" on the value.
Or, in the Xcode4 menu Product, Editscheme, Diagnostics Settings window, simply tick enable zombieobjects, Xcode can be cmd+shift+< into this window.
XCODE4 has taken into account the current requirements, so provides a more convenient way to set up, you can also set some other parameters in this window, you can certainly get more help information.
In addition, if you do not set nszombieenable for Xcode, the following code may be executed correctly, printing out the results you expect "Hello"
[CPP]View Plaincopyprint?
- Static Nsmutablearray*array;
- -(void) Viewdidload
- {
- [Super Viewdidload];
- array= [[Nsmutablearray alloc]initwithcapacity:5];
- [Array release];
- [Array addobject:@"Hello"]; //The reason is not crash, is that the event cycle is not complete, the memory recovery mechanism has not been executed, there is no real recovery of the object memory array.
- NSLog (@"%@", [array objectatindex:0]);
- }
However, once the nszombieenable setting is added, the above line of code [array addobject:@ "Hello"] will not be able to be opportunistic, the same error will be prompted:
-[__nsarraym Addobject:]:message sent to deallocated instance 0x6557370
Even if the array points to the memory or the original data can not escape the nszombieenable of the discernment. That is, the above code can get the correct result when the nszombieenable is not set, because, although [array release] is marked to release the memory block, but the subsequent use of the array, because the pointer to the memory data is not overwritten, so there is no error, this and C + + The effect is the same after the delete of the pointer.
Finally remind nszombieenabled can only be used when debugging, do not forget in the product release time to remove, because nszombieenabled will not really go to release Dealloc object memory, always open the consequences imaginable, please know!
One way to resolve exc_bad_access errors--nszombieenabled (GO)