Question

This line in the function creates an warning: PerformSelector may cause a memory leak because its selector is unknown. What am I doing wrong?

- (void)connectionDidFinishLoading:(NSURLConnection *)connection {

[_delegate1 performSelector:_selector1 withObject:json];

}

and below is the method performSelector

- (void)HttpRequest:(NSURL*)url PostString:(NSString *)poststring method:(int)method withselector:(SEL)selector withdelegate:(id)delegate
{    

 _responseData = [[NSMutableData alloc] init];
// procedures for parse at desired URL
request = [NSMutableURLRequest requestWithURL:url
                                  cachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData
                              timeoutInterval:5];

// set HTTP method
if (method == 0) {
    [request setHTTPMethod:@"GET"];
    // asks xml response
    [request setValue:@"application/json" forHTTPHeaderField:@"Accept"]; }

_selector1 = selector ;
_delegate1 = delegate ;

[self startConnection];

return;
}
Was it helpful?

Solution

You are doing nothing wrong.The compiler cause warning because it does not know about selector yet. If there is one place you getting this warning then use

#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Warc-performSelector-leaks"
       [_delegate1 performSelector:_selector1 withObject:json];
#pragma clang diagnostic pop

If there are multiple places You can define macro

#define SuppressPerformSelectorLeakWarning(Stuff) \
    do { \
        _Pragma("clang diagnostic push") \
        _Pragma("clang diagnostic ignored \"-Warc-performSelector-leaks\"") \
        Stuff; \
        _Pragma("clang diagnostic pop") \
    } while (0)

and then use macro at all places where warning caused

SuppressPerformSelectorLeakWarning(

   [_delegate1 performSelector:_selector1 withObject:json];
);

call every selector like this and it will supress the warning

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top