One way to solve the EXC_BAD_ACCESS error -- NSZombieEnabled), excbadaccess

Source: Internet
Author: User

One way to solve the EXC_BAD_ACCESS error -- NSZombieEnabled (convert), excbadaccess

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:

[Cpp]View plaincopyprint?
  1. Static NSMutableArray * array;
  2. -(Void) viewDidLoad
  3. {
  4. [SuperviewDidLoad];
  5. Array = [[NSMutableArray alloc] initWithCapacity: 5];
  6. [Array release]; // release the array
  7. }
  8. -(Void) viewWillAppear :( BOOL) animated {
  9. [Array addObject: @ "Hello"]; // use the released array
  10. }

The above code will show an EXC_BAD_ACCESS error, but when I execute Xcode, the error is located in my application: didfinishlaunchingwitexceptions: Method on AppDelegate. If there is a large amount of code, it is very difficult to find specific problems, but it is based on experience.

HoweverNSZombieEnabledEnvironment variables can help us, that isWhen the NSZombieEnabled environment variable is set, an object is converted to _ NSZombie when it is destroyed. After NSZombieEnabled is set, when you send a message to a released object, this object will not Crash or generate an incomprehensible behavior, but release an error message, and then disappear in a predictable way that can generate a debug breakpoint.So we can find the specific or probably released object.

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

* **-[_ NSArray 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.
Xcode3LowerNSZombieEnabledThe setting method 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 theenvironment: vertex +, NSZombieEnabled in Name, and Yes in Value. Make sure that the preceding hooks are selected.

Xcode4SettingsNSZombieEnabledMethod:

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 ZombieObjects in the Product> EditScheme> 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"

[Cpp]View plaincopyprint?
  1. Static NSMutableArray * array;
  2. -(Void) viewDidLoad
  3. {
  4. [Super viewDidLoad];
  5. Array = [[NSMutableArray alloc] initWithCapacity: 5];
  6. [Array release];
  7. [Array addObject: @ "Hello"]; // The reason why it does not crash is that the event cycle is not complete, the memory recycle mechanism is not executed yet, and the object memory of array is not actually recycled.
  8. NSLog (@ "% @", [array objectAtIndex: 0]);
  9. }


However, 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 will 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.

The last reminder is that NSZombieEnabled can only be used during debugging. Do not forget to remove it when the product is released. Because NSZombieEnabled does not actually release the memory of the dealloc object, you can understand the consequences of enabling it all the time!

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.