2012-08-10 63 views
7

可能重复:
performSelector may cause a leak because its selector is unknownperformSelector ARC警告

我在非ARC这段代码中没有错误或警告的工作原理:

- (void)addTarget:(id)target action:(SEL)action forControlEvents:(UIControlEvents)controlEvents 
{ 
    // Only care about value changed controlEvent 
    _target = target; 
    _action = action; 
} 

- (void)setValue:(float)value 
{ 
    if (value > _maximumValue) 
    { 
     value = _maximumValue; 
    } else if (value < _minimumValue){ 
     value = _minimumValue; 
    } 

    // Check range 
    if (value <= _maximumValue & value >= _minimumValue) 
    { 
     _value = value; 
     // Rotate knob to proper angle 
     rotation = [self calculateAngleForValue:_value]; 
     // Rotate image 
     thumbImageView.transform = CGAffineTransformMakeRotation(rotation); 
    } 
    if (continuous) 
    { 
     [_target performSelector:_action withObject:self]; //warning here 
    } 
} 

然而,当我转换为ARC项目,我得到这个警告:

“执行选择器可能会导致泄漏,因为其选择器未知。”

我将不胜感激就如何修改我的代码,相应的想法..

回答

40

我发现,以避免该警告的唯一方法是抑制它。你可以在你的构建设置中禁用它,但我更喜欢只使用编译指示来禁用它,因为我知道它是虚假的。

#  pragma clang diagnostic push 
#  pragma clang diagnostic ignored "-Warc-performSelector-leaks" 
      [_target performSelector:_action withObject:self]; 
#  pragma clang diagnostic pop 

如果你要在几个地方的错误,你可以定义一个宏,以使其更容易抑制警告:

#define SuppressPerformSelectorLeakWarning(Stuff) \ 
    do { \ 
     _Pragma("clang diagnostic push") \ 
     _Pragma("clang diagnostic ignored \"-Warc-performSelector-leaks\"") \ 
     Stuff; \ 
     _Pragma("clang diagnostic pop") \ 
    } while (0) 

您可以使用宏是这样的:

SuppressPerformSelectorLeakWarning([_target performSelector:_action withObject:self]); 
+0

感谢罗布。你知道这是否有雷达?大卫 – 2012-08-10 04:36:55

+0

可能是相关的:http://stackoverflow.com/questions/11875900/crash-in-objc-retain-in-method-performed-with-performselector – Jessedc 2012-08-10 04:49:22

+0

@DavidDelMonte我还没有提交它的雷达。我不知道其他人可能提交了哪些雷达。 – 2012-08-11 18:00:07