Find and resolve zombie objects inside iOS

Source: Internet
Author: User

A memory leak is when an object or variable is not freed when it is finished, so what if we go in another extreme situation? This results in an over release problem, which causes the object to be "zombie" and the object to be called a Zombie (Zombies) object. An object has been released, or the caller does not have ownership of the object to release it, causing excessive release, resulting in zombie objects. An attempt to invoke a Zombie object method application crashes (the app jumps directly) and throws an exception exec_bad_access, so the hint given by exec_bad_access is usually a message with no debug value.

A simple sentence description:

Zombie object: An object that has been recycled, or that has an object's memory address already unavailable, is called a Zombie object. Zombie Object Not available

The following is a problematic code

1- (void) TableView: (UITableView *) TableView Didselectrowatindexpath: (Nsindexpath *) Indexpath2  3 { 4  5Nsuinteger row =[Indexpath row];6  7Nsdictionary *rowdict =[Self.listteams Objectatindex:row];8  9NSString *rowvalue = [rowdict objectforkey:@"name"]; Ten   OneNSString *message = [[NSString alloc] initwithformat:@ "You have selected%@ team. ", Rowvalue]; A   -Uialertview *alert = [[Uialertview alloc]initwithtitle:@ "Please select Team" Message:messageDelegate: Self cancelbuttontitle:@ "OK" otherbuttontitles:nil];  -   the [Alert release];  -   - [Message release]; -   + [Alert show];  -   + [TableView Deselectrowatindexpath:indexpath animated:yes]; A   at}
View Code

The result of running the program throws a exec_bad_access exception.

How to debug:

The nszombieenabled variable is used to debug memory-related problems and to track the release process of an object. With nszombieenabled enabled, it replaces the default Dealloc implementation with a zombie, which translates the object into a Zombie object when the reference count drops to 0 o'clock. The purpose of a zombie object is that when you send a message to it, it displays a log and automatically jumps into the debugger.

So, when Nszombie is enabled in the app instead of having the app crash directly, an erroneous memory access becomes an unrecognized message to the zombie object. The Zombie object displays the received information and then jumps into the debugger so you can see exactly where the problem is.

You can set the nszombieenabled environment variable in the Xcode scheme page. Click Product-->edit Scheme to open the page, and then tick the Enable Zombie Objects check box.


Zombies have little effect before the arc appears. But since arc, if you pay more attention to the object's ownership, you usually don't run into memory-related crashes.

Find and resolve zombie objects inside iOS

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.