method_getImplementation在64位iphone 5s上崩溃

时间:2015-03-09 12:48:17

标签: ios objective-c xcode cocoa-touch arm64

我的项目中有一段代码

- (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;
    }
}

这是第一种方法的召唤:

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];

但它崩溃了(显示没有记录):

return grannyImp([class_name class], selector, count, [class_name class]);

PS:当我将arm64架构包含在项目的有效架构部分时,它会崩溃。但是当我离开这个没有arm64的部分时,它运行没有问题。

我做的代码有问题吗?

enter image description here

1 个答案:

答案 0 :(得分:6)

关键字IMP存在问题 实际上IMP使用原型定义了一个函数:id (*)(id, SEL, ...)

在Arm64下,将参数传递给具有可变参数计数的函数不同于它在Arm6和7下的参数

而不是IMP你应该使用你的功能的确切原型 使用此类型:
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。

相关问题