我在Objective-C中使用ARC编写了一个Button类 - 如何防止Clang在选择器上的内存泄漏警告? [英] I'm writing a Button class in Objective-C with ARC -- How do I prevent Clang's memory leak warning on the selector?

查看:241
本文介绍了我在Objective-C中使用ARC编写了一个Button类 - 如何防止Clang在选择器上的内存泄漏警告?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了一个简单的按钮类,像这样:

I'm writing a simple button class, something like this:

@interface MyButton : NSObject {
  id object;
  SEL action;
}
@property(strong) id object;
@property SEL action;
-(void)fire;
@end


@implementation MyButton

@synthesize object, action;

-(void)fire {
  [object performSelector:action];
}

@end

Clang on [object performSelector:action]

I get the following warning from Clang on [object performSelector:action]:

PerformSelector may cause a leak because its selector is unknown

一些研究我发现选择器可以属于具有不同内存要求的系列。意图是行动返回void,所以它不应该引起任何ARC的困难,应该适合 none 家庭。

After some research I see that selectors can belong to families which have different memory requirements. The intention is for the action to return void, so it shouldn't cause any ARC difficulties and should fit in the none family.

看起来像我想要的预处理器代码的相关片段,或者是以下的一个变体:

It looks like the relevant piece of preprocessor code I want is, or is a variant of:

__attribute__((objc_method_family(none)))

但是我在哪里可以告诉Clang不要担心? / p>

But where do I put that to tell Clang not to worry?

推荐答案

在Xcode 4.2中的LLVM 3.0编译器中,您可以按如下方式禁止警告:

In the LLVM 3.0 compiler in Xcode 4.2 you can suppress the warning as follows:

#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Warc-performSelector-leaks"
    [object performSelector:action];
#pragma clang diagnostic pop

感谢Scott Thompson href =http://stackoverflow.com/questions/7017281> performSelector可能会导致泄漏,因为其选择器未知)的答案。

Thanks to Scott Thompson (about this similar question: performSelector may cause a leak because its selector is unknown) for the answer.

这篇关于我在Objective-C中使用ARC编写了一个Button类 - 如何防止Clang在选择器上的内存泄漏警告?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆