public
Last active

Solves the problem of .cxx_destruct methods not being called when zombies are enabled

  • Download Gist
NSObject+ARCZombie.m
Objective-C
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
#import <objc/runtime.h>
 
@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

This problem should be fixed in a future release

@0xced Looks like iOS 5 fixed some of the cleanups for zombies but not all. Should be fixed in a future iOS release.

This bug should be fixed in iOS 6 and OS X 10.8 according to Technical Q&A QA1758.

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.