PerformSelector warning

Blazer picture Blazer · Jan 7, 2012 · Viewed 13.8k times · Source

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

//
}

Answer

ott-- picture ott-- · Jan 8, 2012

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