'iOS performSelector may cause a leak' warning

September 7, 2015

'iOS performSelector may cause a leak' warning

PerformSelector may cause a leak because its selector is unknown - you might have seen this warning in your Xcode, once you attempted to pass SEL object inside performSelector: method like this:

SEL selector = @selector(someMethod);
[self performSelector:selector];

This is due to the fact, that compiler can't tell what is the return type of selector you are using, thus it is not sure that ARC will handle everything correctly.
But we are tough buys, we are not afraid of warning, as long as we know what we are doing, right? So here is a useful way to suppress that warning. Simply write this compiler directive around PerformSelector method:

#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Warc-performSelector-leaks"
    [self performSelector:selector];
#pragma clang diagnostic pop

In case you want to make it look better, we could enclose this in a macros, that would look like this:

#define SuppressPerformSelectorLeakWarning(code) \
    _Pragma("clang diagnostic push") \
    _Pragma("clang diagnostic ignored \"-Warc-performSelector-leaks\"") \
    code; \
    _Pragma("clang diagnostic pop") \

This would result in your code looking like this:

SuppressPerformSelectorLeakWarning([self performSelector:selector])

Comments

comments powered by Disqus