2012-08-10 36 views
7

thể trùng lặp:
performSelector may cause a leak because its selector is unknownperformSelector ARC cảnh báo

Tôi có mã này trong phi ARC mà làm việc mà không có lỗi hoặc cảnh báo:

- (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 
    } 
} 

Tuy nhiên, sau khi tôi chuyển thành dự án thành ARC, tôi nhận được cảnh báo này:

"Thực hiện Bộ chọn có thể gây rò rỉ vì bộ chọn của nó không xác định."

Tôi sẽ đánh giá cao ý tưởng về cách sửa đổi mã của mình cho phù hợp ..

Trả lời

40

Cách duy nhất tôi tìm thấy để tránh cảnh báo là ngăn chặn nó. Bạn có thể vô hiệu hóa nó trong các thiết lập xây dựng của bạn, nhưng tôi thích chỉ sử dụng pragmas để vô hiệu hóa nó, nơi tôi biết nó là giả mạo.

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

Nếu bạn nhận được lỗi ở một số nơi, bạn có thể định nghĩa một macro để làm cho nó dễ dàng hơn để ngăn chặn các cảnh báo:

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

Bạn có thể sử dụng các macro như thế này:

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

Cảm ơn Rob. Bạn có biết nếu có một Radar trên này? David –

+0

Có thể có liên quan: http://stackoverflow.com/questions/11875900/crash-in-objc-retain-in-method-performed-with-performselector – Jessedc

+0

@DavidDelMonte Tôi chưa gửi một radar cho nó. Tôi không biết những gì bất cứ ai khác có thể đã gửi. –

Các vấn đề liên quan