Setting NSZombieEnabled for IOS development (46) locate EXC_BAD_ACCESS Error

Source: Internet
Author: User

Crash is often caused by EXC_BAD_ACCESS errors during iOS program development. When such errors occur, Xcode will not give us too much information to locate the error source, only the Delegate application leaves a message like Thread 1: Program received signal: "EXC_BAD_ACCESS", making the problem untraceable.

For example, when you send a message to a released object, the following error occurs: EXC_BAD_ACCESS, release, and autorelease objects. By default, Xcode does not show you the specific code line, and should not use released objects, or release errors.

For example, the code in the UIViewController subclass is as follows:

Static NSMutableArray * array;

-(Void) viewDidLoad
{
[Super viewDidLoad];
Array = [[NSMutableArray alloc] initWithCapacity: 5];
[Array release];
}

-(Void) viewWillAppear :( BOOL) animated {
[Array addObject: @ "Hello"];
} The above Code will show an EXC_BAD_ACCESS error. However, when I run Xcode, an error is located in a row of my application: didfinishlaunchingwitexceptions: Method on AppDelegate, if there is a large amount of code, it is very difficult to find specific problems, but with experience.

However, NSZombieEnabled environment variables can help us. After NSZombieEnabled environment variables are set, an object will be converted to _ NSZombie when it is destroyed. After NSZombieEnabled is set, when you send a message to a released object, this object will not generate a Crash or incomprehensible behavior, but will release an error message, then it disappears in a predictable way that can generate a debug breakpoint, so we can find the specific or probably which object is released incorrectly.

After NSZombieEnabled is set for Xcode, Xcode will clearly locate the row [array addObject: @ "Hello"], and the error message reported in the console is:

* **-[_ NSArrayM addObject:]: message sent to deallocated instance 0x6557370

How to Set NSZombieEnabled? The settings in Xcode3 and Xcode4 are different. The settings in Xcode4 are simple.
The NSZombieEnabled setting method in Xcode3 is as follows:

1. Find Executables in the Groups & Files column on the left of XCode, double-click one of them, or right-click Get Info;
2. Switch to Arguments
3. there are two boxes in total. Add one item in the following Variables to be set in the environment: Point +, enter NSZombieEnabled in Name, and set Value to Yes, make sure that the preceding hooks are selected.

In Xcode4, set NSZombieEnabled:

Click Product> Edit Scheme> Arguments in the Xcode4 menu, and then click "plus sign" to add the NSZombieEnabled parameter to the Environment Variables window. The value following is "YES ".

You can also click Enable Zombie Objects in the Product> Edit Scheme> Diagnostics settings window of Xcode4. Xcode can use cmd + shift + <to enter this window.

 

Xcode4 has taken into account the current requirements, so it provides a more convenient way to set, you can also set other parameters in this window, you can certainly get more help information.

In addition, if NSZombieEnable is not set for Xcode, the following code may be correctly executed to print the expected result "Hello"

Static NSMutableArray * array;

-(Void) viewDidLoad
{
[Super viewDidLoad];
Array = [[NSMutableArray alloc] initWithCapacity: 5];
[Array release];
[Array addObject: @ "Hello"];
NSLog (@ "% @", [array objectAtIndex: 0]);
} But once the NSZombieEnable settings are added, the above code line [array addObject: @ "Hello"] will not be opportunistic, and the error prompt will also be displayed:

* **-[_ NSArrayM addObject:]: message sent to deallocated instance 0x6557370

Even if the array points to the memory or the original data, it cannot escape the NSZombieEnable eye. That is, when NSZombieEnable is not set, the above Code can get the correct result because, although [array release] is marked as releasing the memory block, when array is used later, because the memory data pointed to by the pointer is not overwritten, no error is made, which is the same as that of the C ++ pointer after delete.

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.