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

 oth0037112 发布于 2022-10-26 13:45
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方法... 这可咋么办啊 根本不知道从何搞起..

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

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

1 个回答
  • 你在pop的时候手动移除吧

    2022-10-27 00:52 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有