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> {

//
}
Alex Cio
  • 6,014
  • 5
  • 44
  • 74
Blazer
  • 14,259
  • 3
  • 30
  • 53
  • 1
    possible duplicate of [performSelector may cause a leak because its selector is unknown](http://stackoverflow.com/questions/7017281/performselector-may-cause-a-leak-because-its-selector-is-unknown) – Jerry Coffin Sep 05 '12 at 06:01
  • I think this [good post](http://stackoverflow.com/questions/7017281/performselector-may-cause-a-leak-because-its-selector-is-unknown) explain the problem very well !May it be helpful ! – monjer Jan 30 '13 at 06:11

5 Answers5

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
ott--
  • 5,642
  • 4
  • 24
  • 27
  • 1
    Selectively ignoring the warning is super handy. Thanks. – Megasaur May 10 '12 at 04: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. – Julian F. Weinert Jan 07 '13 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 Oct 24 '13 at 09: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.

  • Thanks but in my app it's returning the NSLOG; Missed Method. This is wrong – Blazer Jan 07 '12 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. –  Jan 07 '12 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 Jan 07 '12 at 23:00
  • "This can be, in general, safely ignored..." unless the selector happens to begin with `new`, `alloc`, `retain`, `copy`, or `mutableCopy` – newacct Mar 25 '15 at 20:06
3

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

Found the solution here

Community
  • 1
  • 1
Alex Cio
  • 6,014
  • 5
  • 44
  • 74
3

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

Community
  • 1
  • 1
EricS
  • 9,650
  • 2
  • 38
  • 34
0

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

#pragma GCC diagnostic ignored "-Wundeclared-selector"
Sudhin Davis
  • 2,010
  • 13
  • 18