PerformSelector warning
Asked Answered
A

5

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

//
}
Albers answered 7/1, 2012 at 21:21 Comment(2)
possible duplicate of performSelector may cause a leak because its selector is unknownSmudge
I think this good post explain the problem very well !May it be helpful !Schreck
H
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.

Hamforrd answered 7/1, 2012 at 21:44 Comment(4)
Thanks but in my app it's returning the NSLOG; Missed Method. This is wrongAlbers
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.Hamforrd
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.Tirzah
"This can be, in general, safely ignored..." unless the selector happens to begin with new, alloc, retain, copy, or mutableCopyPatellate
B
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
Bajaj answered 7/1, 2012 at 23:18 Comment(3)
Selectively ignoring the warning is super handy. Thanks.Unwell
I figured out that it is enough to use #pragma clang diagnostic ignored "-Warc-performSelector-leaks" at the beginning of the class implementation.Bohr
@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...Imposture
H
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.

Hamforrd answered 7/1, 2012 at 21:44 Comment(4)
Thanks but in my app it's returning the NSLOG; Missed Method. This is wrongAlbers
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.Hamforrd
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.Tirzah
"This can be, in general, safely ignored..." unless the selector happens to begin with new, alloc, retain, copy, or mutableCopyPatellate
V
3

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

Verrocchio answered 8/2, 2012 at 22:30 Comment(1)
Why overkill? It's just another way to do the same thing, isn't it?Bohr
P
3

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

Found the solution here

Priddy answered 20/10, 2014 at 10:44 Comment(0)
F
0

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

#pragma GCC diagnostic ignored "-Wundeclared-selector"
Forfar answered 9/6, 2015 at 4:5 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.