Skip to content

Instantly share code, notes, and snippets.

@0xced
Created May 19, 2012 17:52
Show Gist options
  • Star 14 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save 0xced/2731698 to your computer and use it in GitHub Desktop.
Save 0xced/2731698 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
@0xced
Copy link
Author

0xced commented Aug 6, 2012

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.

@0xced
Copy link
Author

0xced commented Aug 18, 2012

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment