method_getImplementation 在 64 位 iphone 5s 上崩溃 [英] method_getImplementation crashes on 64-bit iphone 5s
问题描述
我的项目中有一段代码
- (NSData *)getIvars:(unsigned int *)count from:(id)class_name NS_RETURNS_RETAINED {
@synchronized(self) {
SEL selector = @selector(copyIvarList:from:);
Method grannyMethod = class_getInstanceMethod([class_name class], selector);
IMP grannyImp = method_getImplementation(grannyMethod);
return grannyImp([class_name class], selector, count, [class_name class]);
}
}
- (NSData *)copyIvarList:(unsigned int *)count from:(id)class_name NS_RETURNS_RETAINED {
@synchronized(self) {
Ivar *ret_val_c = class_copyIvarList([class_name class], count);
NSData *ret_val = [[NSData alloc] initWithBytes:ret_val_c length:sizeof(Ivar) * *count];
free(ret_val_c);
return ret_val;
}
}
这是第一个方法的调用:
Here's the call of first method:
Class class_to_anylize = [self superclass]; // some class inherieted from NSObject
unsigned int ivar_count = 0;
NSData *new_var_list = [self getIvars:&ivar_count from:class_to_anylize];
但它崩溃于(不显示日志):
But it crashes at (showing no log):
return grannyImp([class_name class], selector, count, [class_name class]);
PS:当我将 arm64
架构包含到项目的 Valid Architectures 部分时,它会崩溃.但是当我在没有 arm64
的情况下离开这个部分时,它运行没有问题.
PS: It crashes when I include arm64
architecture to the project's Valid Architectures section. But when I leave this section without arm64
it runs without problem.
我做的代码有问题吗?
推荐答案
关键字IMP
有问题.
实际上IMP
定义了一个函数,原型为:id (*)(id, SEL, ...)
.
There is problem with the keyword IMP
.
Actually IMP
defines a function with the prototype:id (*)(id, SEL, ...)
.
在 Arm64 下将参数传递给具有可变参数计数的函数与在 Arm6 和 7 下的方式不同
Under Arm64 passing arguments to a function with a variable argument count is different than how it is under Arm6 and 7
您应该使用函数的精确原型,而不是 IMP
.
使用这种类型:typedef NSData* (*getIvarsFunction)(id, SEL, unsigned int*, Class);
instead of IMP
you should use exact prototype of your function.
Use this type:
typedef NSData* (*getIvarsFunction)(id, SEL, unsigned int*, Class);
您的代码将是:
- (NSData *)getIvars:(unsigned int *)count from:(id)class_name NS_RETURNS_RETAINED {
@synchronized(self) {
SEL selector = @selector(copyIvarList:from:);
Method grannyMethod = class_getInstanceMethod([class_name class], selector);
getIvarsFunction grannyImp = (getIvarsFunction)method_getImplementation(grannyMethod);
return grannyImp([class_name class], selector, count, [class_name class]);
}
}
此代码适用于 arm6、7、64.
This code will work on arm6,7,64.
这篇关于method_getImplementation 在 64 位 iphone 5s 上崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!