IOS-iPhone objective-C exc_bad_access

Source: Internet
Author: User
It is not terrible for iPhone objective-C exc_bad_access to write programs with bugs. For most problems, it is not difficult to find the cause through simple log or code analysis. 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, 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. This article describes a common method for finding 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"]; creates an nsstring object, and then S = [s substringfromindex: [s rangeofstring: @ "A"]. location]; after execution, the created object reference disappears, directly 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 object we initially created. The second reason is that the nsstring object returned from the substringfromindex :( nsuinteger I) method does not need to be released. It is actually an object 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, there is a record in the ntutoreleasepool. However, because we released this object in the previous error, when [pool drain], the NSAID utoreleasepool again called the release method for the s Object recorded by it, but at this time s 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

It is not terrible for iPhone objective-C exc_bad_access to write programs with bugs. For most problems, it is not difficult to find the cause through simple log or code analysis. 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, 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. This article describes a common method for finding 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"]; creates an nsstring object, and then S = [s substringfromindex: [s rangeofstring: @ "A"]. location]; after execution, the created object reference disappears, directly 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 object we initially created. The second reason is that the nsstring object returned from the substringfromindex :( nsuinteger I) method does not need to be released. It is actually an object 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, there is a record in the ntutoreleasepool. However, because we released this object in the previous error, when [pool drain], the NSAID utoreleasepool again called the release method for the s Object recorded by it, but at this time s 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

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.