Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ReactiveCocoa swizzles dealloc in such a way that brokes swizzling of any other lib in some cases. #774

Conversation

rabovik
Copy link
Contributor

@rabovik rabovik commented Aug 30, 2013

Consider example.

  1. Class B is inherited from A and does not implement dealloc, while A does.
  2. ReactiveCocoa adds its own dealloc implementation to B, which calls original dealloc implementation of A.
  3. Some other lib swizzles dealloc in A with its own implementation.
  4. When an object of class B is deallocated only ReactiveCocoa's and original implementations are called, and lib's is not called but it should be.
#import "ReactiveCocoa.h"
#import <objc/runtime.h>
#import <objc/message.h>

@interface A : NSObject @end
@implementation A
- (void)dealloc{
    NSLog(@"A");
}
@end

@interface B : A @end
@implementation B @end

static void swizzleDealloc(Class class, dispatch_block_t block) {
    SEL original = NSSelectorFromString(@"dealloc");
    Method method = class_getInstanceMethod(class, original);
    __block void (*originalIMP)(__unsafe_unretained id, SEL) = NULL;
    id newIMPBlock = ^(__unsafe_unretained id obj) {
        block();
        if (originalIMP) {
            originalIMP(obj, original);
        }else{
            struct objc_super superInfo = {obj,class_getSuperclass(class)};
            objc_msgSendSuper(&superInfo, original);
        }
    };
    originalIMP = (void *)method_getImplementation(method);
    originalIMP = (void *)class_replaceMethod(class,
                                              original,
                                              imp_implementationWithBlock(newIMPBlock),
                                              method_getTypeEncoding(method));
}
@autoreleasepool {
    id b = [B new];
    [[b rac_willDeallocSignal] subscribeCompleted:^{
        NSLog(@"RAC");
    }];
    swizzleDealloc([A class], ^{
        NSLog(@"Swizzled");
    });
}
// Actual   log: RAC, A
// Expected log: RAC, Swizzled, A

rabovik/ReactiveCocoa@510474e fixes this issue.

…ass that does not implement it leaded to possible conflicts with third-party dealloc swizzling.
@jspahrsummers
Copy link
Member

Thanks for catching this and submitting a fix! However:

  1. I'd really like this to be unit tested.
  2. class_getInstanceMethod shouldn't be used if we really only care about the immediate class anyways.

To that end, I've submitted an alternative fix in #775. It'd be awesome if you could take a look at it and let me know if you see any problems! ✨

rabovik referenced this pull request in th-in-gs/THObserversAndBinders Sep 7, 2013
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants