简体   繁体   中英

How to release the NSUrlConnection class object,NSMutableData class object?

iam making one applciation.In that iam using the NSUrlconnection class.Below one is my code.

- (void)viewDidLoad {
[super viewDidLoad];
responsedata = [[NSMutableData data] retain];
NSString *url = [NSString stringWithFormat:@"https://www.google.com"];
NSURL *URL = [NSURL URLWithString:url];
NSURLRequest *request=[[NSURLRequest alloc]initWithURL:URL];
[[NSURLConnection alloc] initWithRequest:request delegate:self];

[request release];
}

-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response 
{

   [responsedata setLength:0];
}
 - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
[responsedata appendData:data];
   }

  - (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
    }
  - (void)connectionDidFinishLoading:(NSURLConnection *)connection {

     [connection release];
    }

In this code when iam executing,it shows the memory leak at responsedata = [[NSMutableData data] retain]; [[NSURLConnection alloc] initWithRequest:request delegate:self]; in viewDidLoad() .SO please tell me where it released.

  1. You should save reference to your NSURLConnection :

    NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];

  2. You should start it:

    [connection start];

  3. And you should release it in didFailWithError or connectionDidFinishLoading .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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