Permalink
Browse files

Better fix for ARC-related overrelease of spies.

- Thanks Dmitriy for figuring this out.
  • Loading branch information...
1 parent 86124b3 commit 2d2a77b9e6bfe8a87d0813ef33a4b69a0db921a3 Adam Milligan & Sam Coward & Dmitriy Kalinin committed Oct 10, 2012
Showing with 27 additions and 13 deletions.
  1. +27 −13 Source/Doubles/CDRSpy.mm
View
@@ -12,25 +12,39 @@ + (void)interceptMessagesForInstance:(id)instance {
CedarDoubleImpl *cedar_double_impl = [[[CedarDoubleImpl alloc] initWithDouble:instance] autorelease];
objc_setAssociatedObject(instance, @"cedar-double-implementation", cedar_double_impl, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
- NSUInteger originalRetainCount = [instance retainCount];
object_setClass(instance, self);
- NSInteger shortfall = originalRetainCount - [instance retainCount];
+}
- while (shortfall-- > 0) {
- [instance retain];
- }
+- (id)retain {
+ __block id that = self;
+ [self as_original_class:^{
+ [that retain];
+ }];
+ return self;
}
-- (void)dealloc {
- object_setClass(self, objc_getAssociatedObject(self, @"original-class"));
+- (oneway void)release {
+ __block id that = self;
+ [self as_original_class:^{
+ [that release];
+ }];
+}
- [self dealloc];
+- (id)autorelease {
+ __block id that = self;
+ [self as_original_class:^{
+ [that autorelease];
+ }];
+ return self;
+}
- // DO NOT call the destructor on super, since the superclass has already
- // destroyed itself when the original class's destructor called [super dealloc].
- // This (no-op) line must be here to prevent the compiler from helpfully
- // generating an error that the method has no [super dealloc] call.
- if(0) { [super dealloc]; }
+- (NSUInteger)retainCount {
+ __block id that = self;
+ __block NSUInteger count;
+ [self as_original_class:^{
+ count = [that retainCount];
+ }];
+ return count;
}
- (NSString *)description {

0 comments on commit 2d2a77b

Please sign in to comment.