码迷,mamicode.com
首页 > 其他好文 > 详细

调用objc_msgSend方法在64位下崩溃解决方法

时间:2014-11-05 17:42:54      阅读:209      评论:0      收藏:0      [点我收藏+]

标签:des   style   http   io   color   ar   os   使用   for   

之前一直在非64位机器下测试一切正常的程序,在iPhone5s下无缘无故崩溃。崩溃的位置是调用objc_msgSend时出现。经过一番辛苦搜索终于发现苹果官网上有一段这样的描述:

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, thedoSomething: 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_msgSendfunction is cast to a function pointer, the call is dispatched through that same function pointer.

Listing 2-14  Using a cast to call the Objective-C message sending functions

- (int) doSomething:(int) x { ... }
- (void) doSomethingElse {
   int (*action)(id, SEL, int) = (int (*)(id, SEL, int)) objc_msgSend;
   action(self, @selector(doSomething:), 0);
}


貌似是说不能直接使用objc_msgSend的原型方法来匿名调用,否则会出现异常。结果尝试了上面的方法强制转换成一定的方法后,再次运行没有崩溃了,Luck!!

原文来自:https://developer.apple.com/library/ios/documentation/General/Conceptual/CocoaTouch64BitGuide/ConvertingYourAppto64-Bit/ConvertingYourAppto64-Bit.html



调用objc_msgSend方法在64位下崩溃解决方法

标签:des   style   http   io   color   ar   os   使用   for   

原文地址:http://my.oschina.net/vimfung/blog/341119

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!