IPhone objective-C exc_bad_access

Source: Internet
Author: User

From: http://blog.csdn.net/diyagoanyhacker/archive/2011/05/30/6455244.aspx

Write Program It is not terrible to encounter bugs. Most problems are solved through simple log or Code The analysis is not difficult to find the cause. 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, in objective-C
When the problem of exc_bad_access is encountered in programming, it is difficult to find the problem through simple and conventional means. This articleArticleTo introduce you to a common search
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 "]; An nsstring object is created, and the subsequent S = [s substringfromindex: [s
Rangeofstring: @ "A"]. Location]; after execution, the reference of the created object disappears, 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 we initially created
Object. The second reason is that the nsstring returned from the substringfromindex :( nsuinteger I) Method
Object, which is 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
Records already exist in the NSAID utoreleasepool. However, since we released this object in the previous error, when [pool drain]
Now, the nyothoreleasepool calls the release method again for the s object it records.
If it 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.