9

I'm receiving a warning

PerformSelector may cause a leak because its selector is unknown

In the code:

- (void) callDelegate: (SEL) selector withArg: (id) arg error: (NSError*) err
{
    assert([NSThread isMainThread]);
    if([delegate respondsToSelector: selector])
    {
        if(arg != NULL)
        {
            //this line the warning
            [delegate performSelector: selector 
                           withObject: arg 
                           withObject: err]; 
        }
        else
        {
            //this line the warning
            [delegate performSelector: selector 
                           withObject: err]; 
        }
    }
    else
    {
        NSLog(@"Missed Method");
    }
}

Header:

@interface Topscore : UIViewController <NSObject> {

//
}
2

5 Answers 5

43

Your if ... respondsToSelector: selector won't work because your selector is just the name of the method. For your case you need to check

if ([delegate respondsToSelector: @selector(method::)]

and for the other case just for method:.

Anyway, you can supress the warning like this:

#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Warc-performSelector-leaks"
    [self performSelector:nextView];
#pragma clang diagnostic pop
3
  • 1
    Selectively ignoring the warning is super handy. Thanks.
    – Megasaur
    Commented May 10, 2012 at 4:12
  • 5
    I figured out that it is enough to use #pragma clang diagnostic ignored "-Warc-performSelector-leaks" at the beginning of the class implementation. Commented Jan 7, 2013 at 14:58
  • 2
    @Julian but then you don't know if there is a real issue somewhere else in your class implementation. On the other hand you can disable the warning globally in your build settings if you don't mind...
    – Klaas
    Commented Oct 24, 2013 at 9:37
4

This is a warning generated by the compiler because -Wundeclared-selector was used while compiling and automatic reference counting (ARC) is enabled. This can be, in general, safely ignored, as it's obvious that the selector in the variable named "selector" is unknown at compile time, as it will have its value assigned at runtime.

4
  • Thanks but in my app it's returning the NSLOG; Missed Method. This is wrong
    – Blazer
    Commented Jan 7, 2012 at 21:53
  • That means the object does not implement that method (thus does not respond to the specified selector). It does NOT cause a memory leak then, because if the "else" branch of the if structure is reached, the -[delegeta performSelector] method hasn't been invoked, thus it couldn't retain the object, so no memory leaks.
    – user529758
    Commented Jan 7, 2012 at 22:01
  • Did you remember putting a colon behind the selector for each argument? The selector for -(void)methodWithObject:(id)arg1; actually is @selector(methodWithObject:). The colon is part of it.
    – weezor
    Commented Jan 7, 2012 at 23:00
  • "This can be, in general, safely ignored..." unless the selector happens to begin with new, alloc, retain, copy, or mutableCopy
    – newacct
    Commented Mar 25, 2015 at 20:06
3

You can also use objc_msgSend instead of performSelector, as described here.

1
  • Why overkill? It's just another way to do the same thing, isn't it? Commented Oct 24, 2013 at 11:23
3

You could add -Wno-arc-performSelector-leaks for WARNING_CFLAGS in the Build Settings. enter image description here

Found the solution here

0

Easiest way is adding this macro to your pch File. Or .m file..

#pragma GCC diagnostic ignored "-Wundeclared-selector"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.