我正在使用objective-c运行时并在尝试打印对象的描述时获取SIGSEGV:
#include <objc/runtime.h>
#include <stdio.h>
#include <stdlib.h>
int
main()
{
// SEL sAlloc = sel_registerName("alloc");
SEL sInit = sel_registerName("init");
SEL sDesc = sel_registerName("description");
id desc;
Class nAuto = (Class)objc_getClass("NSAutoreleasePool");
Class nObject = (Class)objc_getClass("NSObject");
// Avoid __NSAutoreleaseNoPool warrning
id nsAuto = class_createInstance(nAuto, 0);
objc_msgSend(nsAuto, sInit);
id ns = class_createInstance(nObject, 0);
objc_msgSend(ns, sInit);
desc = objc_msgSend(ns, sDesc);
printf("%s\n", class_getName(ns->isa));
printf("%s\n", class_getName(desc->isa)); // SIGSEGV triggered
NSLog(desc);
return EXIT_SUCCESS;
}
所以,如果有人知道为什么会这样。感谢
答案 0 :(得分:0)
objc_msgSend
必须在使用前进行投射:
desc = (IMP)objc_msgSend(ns, sDesc);