Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

hook dealloc方法装逼失败,咋弄了这下...(该如何收场)

2017-03-13 12:47:33.978 JJYSPlusPlus[9734:5104425] -[WYPlaceholderTextView removeObserver:]: unrecognized selector sent to instance 0x7ffb4e293e00
(lldb)
+ (void)load {
    // Swizzle dealoc to remove observer when current object is deallocated.
    Class classToSwizzle = [UITextView class];
    //    NSString *className = NSStringFromClass(classToSwizzle);
    SEL deallocSelector = sel_registerName("dealloc");

    __block void (*originalDealloc)(__unsafe_unretained id, SEL) = NULL;

    id newDealloc = ^(__unsafe_unretained id objSelf) {
        [objSelf removeObserver:self];

        if (originalDealloc == NULL) {
            struct objc_super superInfo = {
                .receiver = objSelf,
                .super_class = class_getSuperclass(classToSwizzle)
            };

            void (*msgSend)(struct objc_super *, SEL) = (__typeof__(msgSend))objc_msgSendSuper;
            msgSend(&superInfo, deallocSelector);
        } else {
            originalDealloc(objSelf, deallocSelector);
        }
    };

    IMP newDeallocIMP = imp_implementationWithBlock(newDealloc);

    if (!class_addMethod(classToSwizzle, deallocSelector, newDeallocIMP, "v@:")) {
        // The class already contains a method implementation.
        Method deallocMethod = class_getInstanceMethod(classToSwizzle, deallocSelector);

        // We need to store original implementation before setting new implementation
        // in case method is called at the time of setting.
        originalDealloc = (void(*)(__unsafe_unretained id, SEL))method_getImplementation(deallocMethod);

        // We need to store original implementation again, in case it just changed.
        originalDealloc = (void(*)(__unsafe_unretained id, SEL))method_setImplementation(deallocMethod, newDeallocIMP);
    }
}

  • 在一个详情里使用了一个带有placeholder的TextView,pop详情时走了dealloc方法,也就是TextView的dealloc里移除了监听placeholder的通知,然而这个时候莫名其妙的crash了,查找之下,发现是一个第三方组组件里hook了delloc方法... 这可咋么办啊 根本不知道从何搞起..

  • 把这个第三方组件删了.显然这是要有大动作...

请问各位大佬们有木有一套行之有效,无痛解决的方法....在线等,急!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

你在pop的时候手动移除吧


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...