objc_msgSend arm64 崩溃问题
2024-09-03 23:30:39
http://blog.csdn.net/chenyong05314/article/details/42121001
转载自: http://iloss.me/post/kai-fa/2014-12-09-objc_msgsend
之前一直用objc_msgSend,但是没注意apple的文档提示,所以突然objc_msgSend crash了。
之前32位的时候没问题,然后转换为64位之后就会发生EXC_BAD_ACCESS问题。
当然apple再文档(64-Bit Transition Guide for Cocoa Touch中有)中也有提到:
Dispatch Objective-C Messages Using the Method Function’s Prototype
An exception to the casting rule described above is when you are calling the objc_msgSend function or any other similar functions in the Objective-C runtime that send messages. Although the prototype for the message functions has a variadic form, the method function that is called by the Objective-C runtime does not share the same prototype. The Objective-C runtime directly dispatches to the function that implements the method, so the calling conventions are mismatched, as described previously. Therefore you must cast the objc_msgSend function to a prototype that matches the method function being called.
Listing 2-14 shows the proper form for dispatching a message to an object using the low-level message functions. In this example, the doSomething: method takes a single parameter and does not have a variadic form. It casts the objc_msgSend function using the prototype of the method function. Note that a method function always takes an id variable and a selector as its first two parameters. After the objc_msgSend function is cast to a function pointer, the call is dispatched through that same function pointer
你必须先定义原型才可以使用,这样才不会发生崩溃
id Fun(int x,id y,...);
id (*action)(int,id) = (id (*)(int,id)) Fun;
action(1,@"s");
如果没有返回值id改为void
原来我们这么写:
objc_msgSend(self,@selector(doSomething:), 0);
按照apple的规范:
- (int) doSomething:(int) x { ... }
- (void) doSomethingElse {
int (*action)(id, SEL, int) = (int (*)(id, SEL, int)) objc_msgSend;
action(self, @selector(doSomething:), 0);
}
最终简化之后64位调用:
((void(*)(id, SEL,int))objc_msgSend)(self, @selector(doSomething:), 0);
最新文章
- java中对象多态时成员变量,普通成员函数及静态成员函数的调用情况
- mysql查询当前正在使用数据库
- Android手机_软件安装目录
- java输出流实现文件下载
- lk启动流程详细分析
- ubuntu上如何安装和卸载google chrome 浏览器
- 经典SQL语句大全之数据开发
- 改变和恢复view的方向
- linux下面的解压缩文件的命令
- api-gateway实践(09)支持rest服务注册
- Deployment descriptor
- 每日英语:The Right Way to Network
- vue使用render渲染&;jsx
- HTTP 权威指南 第二章 URL 与资源
- Mac OS 10.12 - Gogland和在Windows中使用的不同!!
- [日常] crontab的秒执行和串行化和多进程实现
- -174dBm的含义
- Problem Z: 百鸡问题
- WordCounter Python实现
- Markdown博文快速转为微信文章