What is a Zombie object? The so-called Zombie, is the object of excessive release. In iOS development, zombie objects are useful for developers debugging programs. We usually set the nszombieenabled environment variable to Yes to open the Zombie object, but this causes all objects to be freed and the program to run for long periods of memory.
Is there any other way to implement zombie objects? The following small series to introduce to you, imitate Xcode with code to implement the Zombie object method.
Creating Zombie Objects
In iOS development, when the retaincount of a normal object becomes 0, the dealloc is called, and the code is checked to Dealloc, and then it can be done:
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 method, because the zombie class does not have its own method, so it 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:.
The Zombie class implementation needs to meet the details
1, can not like the realization of ordinary class inherited from the NSObject, or we inherited a lot of nsobject in the method, it is not able to intercept the 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 the zombie class did not implement this method, then will be forwardinvocate:.
method to get the original class name
Although we can intercept this message in Methodsignatureforselector: But the object's ISA pointer has already pointed to the zombie class, how to get the name of the original class? There is a clever way to create this new zombie class name to use the rules named: nszombie+ The original class name, in the Methodsignatureforselector: The prefix can be nszombie removed, get the original class name.
The implementation of the Zombie class
@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, 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:nsindexsetsize: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:nsindexsetsize:16, Classname:_nszombie_nsindexset
These are the zombie class creation, implementation, usage-related knowledge, learning and mastering this knowledge, for iOS developers is very important, do not know how to create zombie objects you learned? Of course, it is also welcome to share the easier way to implement zombie objects.
How to implement zombie objects in iOS