首页 > 代码库 > 调用objc_msgSend方法在64位下崩溃解决方法

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

之前一直在非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位下崩溃解决方法