34

Possible Duplicate:
performSelector may cause a leak because its selector is unknown

I did a NSDictionary to convert my input(NSString) to selector. The "selector map" is looked as follows :

[self setCmdSelectorMap:[NSDictionary dictionaryWithObjectsAndKeys: 
                         [NSValue valueWithPointer:@selector(doOpenBrowserByString:)], @"openBrowser",
                         [NSValue valueWithPointer:@selector(syncData:)], @"sync",
                         [NSValue valueWithPointer:@selector(getCachedString:)], @"getCachedString",
                         nil]];

When I try to fetch one of these selector and perform it by follows, it cause a warning :

sel = [[_cmdMap objectForKey:command] pointerValue];
NSLog(@"selector determined : %@", NSStringFromSelector(sel));
[self performSelector:sel withObject:arguments];

The warning says : PerformSelector may cause a leak because its selector is unknown. Is there any way to prevent this warning from occurring? or is there any "safer" way to perform such an action?

Thanks guys :)

Community
  • 1
  • 1
Rayer
  • 358
  • 1
  • 3
  • 6
  • 2
    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) [performSelector: warning](http://stackoverflow.com/questions/8773226/performselector-warning), [Why warning with performSelector:](http://stackoverflow.com/questions/10531119/), [How do I prevent Clang's warning on performSelector:?](http://stackoverflow.com/questions/7043999/) – jscs May 29 '12 at 04:38
  • @JacquesCousteau wherever do you find the time to get all of these? Anyhow, you're on a roll with the duplicates, man. Keep it up! – CodaFi May 29 '12 at 04:44
  • @Coda: I just typed ["\[objc\] performSelector may cause leak"](http://stackoverflow.com/search?q=%5Bobjc%5D+performSelector+may+cause+leak) into the search box and looked at the first page of results, but I appreciate the appreciation. – jscs May 29 '12 at 04:46
  • Thank guys, although the reason of warning is not exactly the same, but I have known this is supposed to safe to ignore. I hope our project is a warning-free one, so I will make compiler just ignore it. Thanks again! – Rayer May 29 '12 at 06:13

1 Answers1

130

Just use this:

#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Warc-performSelector-leaks"
[self performSelector:sel withObject:arguments];
#pragma clang diagnostic pop
Alexander Zakatnov
  • 1,646
  • 1
  • 15
  • 16
  • Thanks, this worked perfectly and makes complete sense – migs647 Jan 02 '13 at 20:39
  • 7
    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:57
  • 12
    That is true, @Julian, yet by using the push/pop instructions you can ensure to only ignore the warning in places that you have reviewed. I'd think of it as good practice. – epologee Feb 04 '13 at 13:03
  • 4
    While a valid way to remove the warnings, I discourage silencing warnings. See my answer on the linked duplicate for reasoning. – wbyoung Feb 07 '14 at 20:04