利用objc的runtime来定位次线程中unrecognized selector sent to instance的问题

简介:

昨天遇到一个只有一行错误信息的问题:

-[NSNull objectForKey:]: unrecognized selector sent to instance 0x537e068

由于这个问题发生在次线程,所以没有太有用的堆栈信息,而是只有简单的SIGABRT信息:



考虑到unrecognized selector sent to instance这类问题是由于向某个对象发送了未实现的消息,这个过程大致如下(图片摘自这里):


参考Objective-C的对象模型:

struct objc_class {
    Class isa  OBJC_ISA_AVAILABILITY;

#if !__OBJC2__
    Class super_class                                        OBJC2_UNAVAILABLE;
    const char *name                                         OBJC2_UNAVAILABLE;
    long version                                             OBJC2_UNAVAILABLE;
    long info                                                OBJC2_UNAVAILABLE;
    long instance_size                                       OBJC2_UNAVAILABLE;
    struct objc_ivar_list *ivars                             OBJC2_UNAVAILABLE;
    struct objc_method_list **methodLists                    OBJC2_UNAVAILABLE;
    struct objc_cache *cache                                 OBJC2_UNAVAILABLE;
    struct objc_protocol_list *protocols                     OBJC2_UNAVAILABLE;
#endif

} OBJC2_UNAVAILABLE;
/* Use `Class` instead of `struct objc_class *` */

消息发送的流程大致如下:

  • 判断发送的消息是否为retain等内存管理方法;
  • 判断receiver是否为nil;
  • 判断是否在方法缓存中,即struct objc_cache *cache;
  • 判断是否在方法列表中,即struct objc_method_list **methodLists,由于对象的方法可以动态添加,所以这里的类型是struct objc_method_list **,可以参考源文件;
  • 判断是否在继承体系中——到这里,称之为Messaging过程。
  • 如果实在找不到,就执行Dynamic Method Resolution过程,即尝试调用resolveInstanceMethod:或resolveClassMethod:方法,我们可以通过实现这两个方法来动态添加方法;
  • 动态方法解析过程如果返回NO,那么还有最后的拯救机会,就是Message Forwarding消息转发过程(参考NSObject.h):

- (id)forwardingTargetForSelector:(SEL)aSelector;
- (void)forwardInvocation:(NSInvocation *)anInvocation;
- (NSMethodSignature *)methodSignatureForSelector:(SEL)aSelector;

我第一反应是添加resolveInstanceMethod:来观察,这是一个类方法,所以得添加到metaClass上:

Class metaClass = objc_getMetaClass("NSNull");
SEL sel = @selector(resolveInstanceMethod:);
const char *type = "c@::";
class_addMethod(metaClass, sel, (IMP)resolveInstanceMethod, type);

但遗憾的是,即便此时方法寻找不到时会调用到resolveInstanceMethod:方法,不过在设置的断点位置看也已经没有明确的堆栈信息了,所以我就直接添加找不到的方法objectForKey:来定位:

Class metaClass = objc_getMetaClass("NSNull");
SEL sel = @selector(objectForKey:);
const char *type = "@@:@";
class_addMethod(metaClass, sel, (IMP)objectForKey, type);

样一来,通过在我们添加的objectForKey方法中设置断点就可以获取到详细堆栈信息,从而进一步定位到问题所在:
{
  fromId = "\U6d4b\U8bd520#\U65fa\U4f01\U65e0\U7ebf\U6d4b\U8bd5";
  msgContent = "";
  msgSendTime = 1402909302;
  msgType = 12;
  uuid = 0;
}

原因是由于服务端推送的消息中一个必填字段为空,而客户端也刚好在此处没有使用项目代码中约定的类型检查宏(此处应为VFDict),而是直接当做NSDictionary来操作。

目录
相关文章
|
3月前
|
开发工具 git
Stylelint——Unexpected unknown pseudo-class selector ":deep" selector-pseudo-class-no-unknown
新项目制定规范接入了stylelint,并通过husky在git提交时去触发检测修复,使用`:deep()`的时候却发现了报错;
134 1
|
6月前
|
Java 数据库 索引
GreenDao,clearIdentityScope报错Error:Execution failed for task ':app:compileDebugJavaWithJavac'. > Com
GreenDao,clearIdentityScope报错Error:Execution failed for task ':app:compileDebugJavaWithJavac'. > Com
48 1
|
Java Maven Android开发
成功解决FATAL ERROR in native method: JDWP on getting class status, jvmtiError=JVMTI_ERROR_WRONG_PHASE
成功解决FATAL ERROR in native method: JDWP on getting class status, jvmtiError=JVMTI_ERROR_WRONG_PHASE
|
6月前
Error:Execution failed for task ':app:javaPreCompileDebug'. > Annotation processors must be explicit
Error:Execution failed for task ':app:javaPreCompileDebug'. > Annotation processors must be explicit
92 0
|
Java Spring
Redisson BUG: Failed to submit a listener notification task. Event loop shut down?
Redisson BUG: Failed to submit a listener notification task. Event loop shut down?
1387 0
解决performSelector may cause a leak because its selector is unknown警告
采用以下方法时会提示【performSelector may cause a leak because its selector is unknown】警告
210 0
在main函数中创建新对象时出错 No enclosing instance of type ooo is accessible. Must qualify the allocation with a
在main函数中创建新对象时出错 No enclosing instance of type ooo is accessible. Must qualify the allocation with a
在main函数中创建新对象时出错 No enclosing instance of type ooo is accessible. Must qualify the allocation with a
This error might indicate a memory leak if setState() is being called because another object is reta
This error might indicate a memory leak if setState() is being called because another object is reta
unrecognized selector sent to instance的一类解决办法
unrecognized selector sent to instance的一类解决办法
141 0
|
测试技术
gradle编译出错:Execution failed for task ':app:compileTestDebugJava'
版权声明:本文为博主原创文章,转载请标明出处。 https://blog.csdn.net/chaoyu168/article/details/50012843 随着更新gradle到1.12,出现问题。
1110 0