开发者

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];开发者_StackOverflow中文版 [[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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜