OS objc_msgSend 报错解决方案 以及 内存泄漏的问题?

objc_msgSend(self, @selector(doSomething), self);====>这个函数使用会报错:Too many arguments to function call, expected 0, have 3
错解决方案:选中项目 - Project - Build Settings - ENABLE_STRICT_OBJC_MSGSEND 将其设置为 NO 即可


xcode配置

之前32位的时候没问题,然后转换为64位之后就会发生EXC_BAD_ACCESS问题。

image

苹果官方文档:

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

你必须先定义原型才可以使用,这样才不会发生崩溃:

int (*action)(id, SEL, int) = (int (*)(id, SEL, int)) objc_msgSend;
action(self, @selector(doSomething:), 0);
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi阅读 7,424评论 0 10
  • 相信大家都有过这样的感受,上学时,下课前的那几分钟往往内心欢喜无法安定,人在课堂心已在路上。走上社会后,情况并没有...
    西门大盗阅读 277评论 0 1
  • 曼陀罗第三天 开机音乐,原来能做到了。 吸收自己的祸从口出。 记得,接的。自己。
    马上做阅读 78评论 0 1
  • 校而不对。当你曾经经行之处,已经渺无痕迹,早已经轻舟过了万重山,却有人记录了你到此一游,集体喷雾,喷喷喷。即使一头...
    jingtu阅读 456评论 0 0