Skip to content

Instantly share code, notes, and snippets.

@hjeffrey
Forked from 0xced/NSObject+ARCZombie.m
Created August 28, 2012 02:45
Show Gist options
  • Save hjeffrey/3494455 to your computer and use it in GitHub Desktop.
Save hjeffrey/3494455 to your computer and use it in GitHub Desktop.
Solves the problem of .cxx_destruct methods not being called when zombies are enabled
#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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment