Solution to IOS memory error exc_bad_access _ios

Source: Internet
Author: User

The most depressing thing about iOS development is that the program crashes without warning, using the BT command to play the call stack, giving a bunch of system exc_bad_access information, and there's no way to locate the problem. First of all, exc_bad_access this error, so to speak, 90% of the source of the error is the release of an object that has been released. As a simple example, let's look at a piece of Java code First:

Copy Code code as follows:

public class test{
public static void Main (string[] args) {
string s = "This is a test string";
s = s.substring (S.indexof ("a"), (S.length ());
System.out.println (s);

}
}


Typically, such crashes occur because of a call to a freed memory space, or a repeated release of an address space. And how to navigate to this address, you can edit Xcode scheme, add the following tag bit, so that the system to print the wrong address, as shown in the figure:

(Through Product->scheme->edit Scheme enter the following edit page, select Arguments tab, increase the mark bit nszombieenabled set to Yes)

In this way, but the crash occurs, the system will receive the following prompt message:

2013-06-23 00:45:20.479 * * *-[__nsarraym AddObject:]: Message sent to deallocated instance

The reason for the crash is that the memory address 0x7179910 is repeatedly released.

Objective-c This code has three fatal problems: 1, memory leakage, 2, error release, 3, causing exc_bad_access error.

If the crash occurs on the current call stack, the system will locate the cause of the crash in the specific code through the above procedure. However, if the crash is not on the current call stack, the system will only be able to tell us the crash address, but not the specific code, so we can not modify the error. You can then modify scheme so that Xcode records the history of each address alloc so that we can use the command to restore the address. Figure: (Add mallocstackloggingnocompact, and set to Yes, as set nszombieenabled )

In this way, when the reason for the crash is message sent to deallocated instance 0x7179910, we can restore the memory address by using the following command:

Info malloc-history 0x7179910

As shown in the figure, this command is able to specifically restore this address in which line of code generation.

(Note that because this command only supports GDB, the console output must be changed to GDB, and a bit regrettable, only the simulator, does not support true machine debugging)

(The same is through Product->scheme->edit Scheme into the above edit page, select Info tab)

In this way, a good review of the line of code, it should be easy to identify the problem.

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.