The implementation of zombie objects in iOS

Source: Internet
Author: User

Zombie objects are useful for our debugger, and the way to open a zombie object in Xcode is to set the nszombieenabled environment variable to Yes, which causes all objects to be freed, the program will run for a long time, and this time we'll write some code, Emulate the implementation in Xcode, so that we can generally understand the principle of implementing a Zombie object in Xcode.


Mike Ash has explained the details of the zombie object implementation in his blog, and I'm here to translate it ... (These links may need to flip the wall ...) )


The objects in OC are structs, the first field in the struct is an Isa, the object's class object, the class object is also an object, and there is an Isa pointer pointing to the meta-class object, which is not detailed here, specifically see the explanation in Cocoawithlove.


When the retaincount of a normal object becomes 0, it calls Dealloc, and our code is to hook up the dealloc, and do the following here:

1, create a new zombie class;

2. Point this object's Isa to the Zombie class (this object becomes the object of the Zombie class)

In this way, all future messages to this object will now go to the zombie class to find the implementation of the method, as mentioned above, the Zombie class does not have its own method, so will forwardinvocate: (nsinvocation*), before this, the system will also be based on the selector of the message, Call the Methodsignatureforselector: method to generate the Nsinvocation object, so the first time to discover the message to the zombie object is in Methodsignatureforselector:.


From the above discussion, you can get the zombie class implementation to meet the following details:

1, can not like the realization of other classes inherit from NSObject, otherwise we inherit a lot of nsobject in the method, also can not be able to intercept this message in Methodsignatureforselector:;

2, need to implement Methodsignatureforselector: method, print out the relevant information here;

3, need to implement the +initialize method, this method is all the class is sent before the first method will be called a method, if our zombie class did not implement this method, then will be forwardinvocate:.


Now there is a problem, although we can intercept this message in Methodsignatureforselector: But the object's Isa pointer is already pointing to the zombie class, how do we get the name of the original class? One ingenious way to create this new zombie class is to use the name of the rule: nszombie+ the name of the original class, in Methodsignatureforselector: You can remove the prefix nszombie prefix, get the original class name.


Mike Ash also has an article on how to implement a Zombie object in Xcode.


First I'm going to give you a piece of code

@implementation nszombie-(ID) init{self    = [super init];    if (self) {        Nsindexset *obj = [[Nsindexset alloc] init];        [Nszombie dump:obj];        [obj release];        [Nszombie dump:obj];    }    return self;} + (void) Dump: (ID) obj{    size_t size = malloc_size (obj);    NSLog (@ "Size:%zu, classname:%s", size, Object_getclassname (obj));} @end


without opening the nszombieenabled, we came to the point that although the object has been freed, but the memory is not replicated, it is still possible to find the corresponding class information through the ISA pointer.

Size:16, Classname:nsindexset

size:0, Classname:nsindexset


After opening the nszombieenabled, you can see that the object reference count becomes 0, and the object's corresponding class has been changed to become a zombie object.

Size:16, Classname:nsindexset

Size:16, Classname:_nszombie_nsindexset


Organized code in Github:zombie

The implementation of zombie objects in 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.