Why is object not dealloc'ed when using ARC + NSZombieEnabled

前端 未结 5 1093
借酒劲吻你
借酒劲吻你 2020-11-30 01:25

I converted my app to ARC and noticed that an object alloc\'ed in one of my view controllers was not being dealloc\'ed when that view controller was dealloc\'ed. It took a w

5条回答
  •  独厮守ぢ
    2020-11-30 01:36

    This is a bug that has been acknowledged by Apple in Technical Q&A QA1758.

    You can workaround on iOS 5 and OS X 10.7 by compiling this code into your app:

    #import 
    
    @implementation NSObject (ARCZombie)
    
    + (void) load
    {
        const char *NSZombieEnabled = getenv("NSZombieEnabled");
        if (NSZombieEnabled && tolower(NSZombieEnabled[0]) == 'y')
        {
            Method dealloc = class_getInstanceMethod(self, @selector(dealloc));
            Method arczombie_dealloc = class_getInstanceMethod(self, @selector(arczombie_dealloc));
            method_exchangeImplementations(dealloc, arczombie_dealloc);
        }
    }
    
    - (void) arczombie_dealloc
    {
        Class aliveClass = object_getClass(self);
        [self arczombie_dealloc];
        Class zombieClass = object_getClass(self);
    
        object_setClass(self, aliveClass);
        objc_destructInstance(self);
        object_setClass(self, zombieClass);
    }
    
    @end
    

    You will find more information about this workaround in my blog post Debugging with ARC and Zombies enabled.

提交回复
热议问题