I am trying to do the following:
- access the class'dealloc IMP
- introduce a custom IMP into the mentioned class, which essentially calls the original dealloc IMP
- When an instance of the specified class is freed, both IMPs must start.
This is my attempt:
@implementation ClassB - (void)dealloc { NSLog(@"\n%@ | %@", self, NSStringFromSelector(_cmd)); } @end @implementation ClassC - (void)swizzleMe:(id)target { SEL originalDeallocSelector = NSSelectorFromString(@"dealloc"); __block IMP callerDealloc = [target methodForSelector:originalDeallocSelector]; const char *deallocMethodTypeEncoding = method_getTypeEncoding(class_getInstanceMethod([target class], originalDeallocSelector)); IMP newCallerDealloc = imp_implementationWithBlock(^(id _caller) { NSLog(@"\n new dealloc | calling block %p for %@", callerDealloc, _caller); callerDealloc(_caller, originalDeallocSelector); }); NSLog(@"\nswapping %p for %p", newCallerDealloc, callerDealloc); class_replaceMethod([target class], originalDeallocSelector, newCallerDealloc, deallocMethodTypeEncoding); } @end
Used like this:
ClassB *b = [[ClassB alloc] init]; ClassC *c = [[ClassC alloc] init]; [c swizzleMe:b];
But the results are:
disabled zombie objects:
2013-07-03 13:24:58.368 runtimeTest[38626:11303] swapping 0x96df020 for 0x2840 2013-07-03 13:24:58.369 runtimeTest[38626:11303] new dealloc | calling block 0x2840 for <ClassB: 0x93282f0> 2013-07-03 13:24:58.370 runtimeTest[38626:11303] <ClassB: 0x93282f0> | dealloc 2013-07-03 13:24:58.370 runtimeTest[38626:11303] new dealloc | calling block 0x2840 for <ClassB: 0x93282f0> 2013-07-03 13:24:58.371 runtimeTest[38626:11303] <ClassB: 0x93282f0> | dealloc runtimeTest(38626,0xac55f2c0) malloc: *** error for object 0x93282f0: pointer being freed was not allocated *** set a breakpoint in malloc_error_break to debug 2013-07-03 13:24:58.371 runtimeTest[38626:11303] new dealloc | calling block 0x2840 for <ClassB: 0x93282f0> 2013-07-03 13:24:58.372 runtimeTest[38626:11303] <ClassB: 0x93282f0> | dealloc
objects with zombies are included (line 11 is EXC_BAD_ACCESS in the picture)
2013-07-03 13:34:37.466 runtimeTest[38723:11303] swapping 0x97df020 for 0x2840 2013-07-03 13:34:37.467 runtimeTest[38723:11303] new dealloc | calling block 0x2840 for <ClassB: 0x715a920> 2013-07-03 13:34:37.468 runtimeTest[38723:11303] <ClassB: 0x715a920> | dealloc
Any thoughts on what I'm doing wrong?

ios objective-c cocoa-touch cocoa
SaldaVonSchwartz
source share