简体   繁体   English

如何在iOS中解决此警告? 内存泄漏

[英]How can I solve this warning in iOS? Memory Leak

This line in the function creates an warning: PerformSelector may cause a memory leak because its selector is unknown. 函数中的这一行会产生警告: PerformSelector可能会导致内存泄漏,因为其选择器未知。 What am I doing wrong? 我究竟做错了什么?

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

[_delegate1 performSelector:_selector1 withObject:json];

}

and below is the method performSelector 下面是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;
}

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 像这样调用每个选择器,它将阻止警告

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM