Debug exc_bad_access note

Source: Internet
Author: User
First encounter Exc_bad_access , And then Program Crash, no debugging information
This is why I checked it online.
When a message is sent to a released object, exc_bad_access is displayed. When an error occurs, stack information is usually called, especially in the case of multiple threads.
Actually, the wild pointer is used.

So I followed the tutorial (http://www.codza.com/how-to-debug-exc_bad_access-on-iphone) to open executables in xcode
Open the arguments panel in the information panel.
Then add ParametersNszombienabled = YesThis parameter value allows GDB to provide a useful information when it finds that released objects are used.
(Zombie reminds me of Plants vs. Zombies ....)

Run again. GDB provides the error message 01:00:00. 299 vdic [342: 20b] ***-[fmresultset release]: Message sent to deallocated instance 0x3d30f70
So far, I know thatExcessive fmresultset release
However, it is unclear where the release is excessive.

ThereforeObjc_exception_throwAnd-[Nsexception raise]Added the breakpoint list.
Run again and locate the error to the main function... Khan... which is too inaccurate.

Why is the main function an incorrect position? I suddenly thought of a memory pool in the main function to maintain the objects using the automatic recycle mechanism.
If the error is found in main, the problem may occur.
Int main (INT argc, char * argv []) {

NSAID utoreleasepool * Pool = [[NSAID utoreleasepool alloc] init];
Int retval = uiapplicationmain (argc, argv, nil, nil );
[Pool release];
Return retval;
}

Further recalling the memory management mechanism of the fundation framework, I obviously release the fmresultset object that should be automatically recycled somewhere.
In this way, I can locate some locations and find them ....

+ (Nsmutablearray *) Select: (nsstring *) SQL;
{
Nsmutablearray * arr = [[nsmutablearray alloc] init];
Fmresultset * rs = [dB executequery: SQL];
While ([RS next]) {
[Arr addobject: [RS resultdict];
}

[RS close];
[RS release];
Return arr;
}

RS is the result returned by other object functions. The reference count is 1 at the beginning and is put into the Auto Release pool.
The release I have learned is the root cause of all disasters. Amen
The statement was successfully executed.

Read out the following cocoa fundation memory release principles

1. count the objects created by assigning or copying them
2. assume that the object obtained by any other method is counted as 1 and is in the Auto Release pool. to use this object outside the current execution range, you must keep it
3. when an object is added to a set, it is maintained and released when the object is removed from the set. releasing a collection object releases all objects in the set
4. make sure that the number of alloc, copy, mutablecopy, or retain messages is the same as the number of release or autorelease messages sent to this object. in other words, make sure that your Code balance
5. set the attribute in the access method, and then release it (ztime: Now there are two Commands: @ propperty and @ synthesize to automatically create this code)
6. use @"... "The nsstring object created by the structure is a constant. sending release or retain is ineffective

In general, the memory management of cocoa fundation is very simple and clear,
Ms's COM Object lifetime management is also a smart pointer :)

After the object is released, the pointer is usually set to nil.

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.