Build Your own Nszombie

Source: Internet
Author: User

When you turn on the xcode zombie option to send a message to a "freed object"

 objzomies *oz = [[Objzomies alloc] init]; Oz.name  = @ " obz   " ; NSLog ( @ " objzomies:----%@---%s---%p  , [objzomies class  ],object_getclassname (oz), Oz);        [Oz release]; NSLog ( @ " objzomies:-------%@----% s----%p  , [objzomies class  ],object_getclassname (oz), Oz); Oz.name  = @ " z----o  ";

Printing results:

 --Ten- -  -: +:43.303zombies[68811:1009530] objzomies:----objzomies---objzomies---0x7fcb71f047c0 --Ten- -  -: +:43.304zombies[68811:1009530] Objzomies:-------objzomies----_nszombie_objzomies----0x7fcb71f047c0 --Ten- -  -: +:43.304zombies[68811:1009530] * * * *-[objzomies setName:]: Message sent to deallocated instance0x7fcb71f047c0

It's amazing .... Analysis log can draw the following information:

1, we sent a setName: method to an "already freed object" objzomies.

2, Object_getclassname (Oz) was printed before and after Oz's release, that is, Oz's Isa-pointing class changed 0.0

Objzomies and _nszombie_objzomies

3, did not notice that the three%p are the same 0x7fcb71f047c0, stating that oz this goods release and did not release.

So "objects that have been freed" are not released but become "zombies" that remain in memory while the program is running. Of course, after you enable the Xcode zombie option, all classes cannot be "spared" to "zombies"

_nszombie_objzomies, of course, is the class that is added to the runtime, like KVO, which generates a special function class xxx_a to perform certain functions, depending on the target class, if it is a.

The _nszombie_ prefix + objzomies constitutes the function class created at runtime to capture "zombies".

_nszombie_myclass , which is capturing zombie classes,

The effect is to catch a mechanism that triggers an exception when a message is sent to an "already freed object", and to remind the developer exactly which class has been released and which message.

This relies on the powerful dynamics of OC, as well as the message passing mechanism.

Simple generalization principle:

When instance a of Class A has a reference count of 0 o'clock, call Delloc for memory cleanup work, of course its base class is NSObject will eventually be transferred to NSObject Dealloc. If you let it go, a will be released. And we never know who it is.

So you can't let a release, let a become a "zombie" that is, A's memory forever resides in the program runtime, create a Catch zombie class B, when the message is sent to B in the forwarding of the exception, and prompts the relevant information. That is a--isa-->b the ISA pointer to B, (here does not understand the need to do the homework well) then a will use ISA to B to find the corresponding implementation.

The implementation is as follows:

Note that the MAIN.M environment is non-arc and turns on Xcode zombie related options

#import<UIKit/UIKit.h>#import "AppDelegate.h"#import<objc/runtime.h>#import "ObjZomies.h"voidEmpthimp (IDObj,sel _cmd) {}nsmethodsignature* Zombiemethodsignatureforselector (IDObj,sel _cmd,sel selector) {Classclass=object_getclass (obj); NSString*classname = Nsstringfromclass (class); ClassName= [ClassName substringfromindex:[@"Cmzombie_"length]]; NSLog (@"Selector%@ sent to deallocated instance%p of class%@", Nsstringfromselector (selector), obj, className);      Abort (); returnNil;} Class Zombifyclass (classclass) {NSString* ClassName = Nsstringfromclass (class); NSString*zombieclassname = [@"Cmzombie_"Stringbyappendingstring:classname]; Class Zombieclass=nsclassfromstring (zombieclassname); if(zombieclass) {returnZombieclass; }//Build your own Nszombie Zombieclass= Objc_allocateclasspair (nil, [Zombieclassname utf8string],0); Class_addmethod (Zombieclass, @selector (methodsignatureforselector:), (IMP) Zombiemethodsignatureforselector,"@@::");
It is easy to understand that +initialize exists in Metaclass, and the instance method exists in class. Class_addmethod (Object_getclass (Zombieclass), @selector (Initialize), (IMP) Empthimp,"[email protected]:"); Objc_registerclasspair (Zombieclass); returnZombieclass;}voidZombiedealloc (IDObj,sel _cmd) {Class C=Zombifyclass (Object_getclass (obj));
Point obj's Isa to our custom "Capture Zombie class" _nszombie_myclass object_setclass (obj, c);}voidEnablezombies (void) {Method m= Class_getinstancemethod ([nsobjectclass], @selector (dealloc)); Method_setimplementation (M, (IMP) zombiedealloc);}

intMainintargcChar*argv[]) {//enable enablezombies (); Objzomies*oz =[[Objzomies alloc] init]; Oz.name=@"Obz"; NSLog (@"objzomies:----%@---%s---%p", [objzomiesclass],object_getclassname (oz), Oz); [Oz release]; NSLog (@"objzomies:-------%@----%s----%p", [objzomiesclass],object_getclassname (oz), Oz); Oz.name=@"z----o"; @autoreleasepool {returnUiapplicationmain (argc, argv, Nil, nsstringfromclass ([appdelegateclass])); }}
#import <Foundation/Foundation.h>@interface*name; @end #import " ObjZomies.h " @implementation objzomies @end

Run again:

 --Ten- -  -: -:55.433zombies[68883:1030347] objzomies:----objzomies---objzomies---0x7f825ae00790 --Ten- -  -: -:55.435zombies[68883:1030347] Objzomies:-------objzomies----cmzombie_objzomies----0x7f825ae00790 --Ten- -  -: -:55.435zombies[68883:1030347] Selector Setname:sent to deallocated instance0x7f825ae00790OfclassObjzomies

So cool.

Reference: https://www.mikeash.com/pyblog/friday-qa-2014-11-07-lets-build-nszombie.html
Http://www.cocoachina.com/ios/20141204/10526.html

Build Your own Nszombie

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.