本文介绍了method_getImplementation在64位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时崩溃架构添加到项目的有效架构部分。但是当我离开本节而没有 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.

我做过任何有问题的代码吗?

Is there any problematic code I done?

推荐答案

关键字 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上崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-12 04:54