【问题标题】:How to release the NSUrlConnection class object,NSMutableData class object?NSUrlConnection类对象,NSMutableData类对象如何释放?
【发布时间】:2011-09-28 09:19:42
【问题描述】:

我正在制作一个应用程序。在那个 iam 中使用 NSUrlconnection 类。下面是我的代码。

- (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];
    }

在此代码中,当 iam 执行时,它显示了内存泄漏 responsedata = [[NSMutableData data] retain]; [[NSURLConnection alloc] initWithRequest:request delegate:self];viewDidLoad() .SO 请告诉我它是在哪里发布的。

【问题讨论】:

    标签: iphone nsurlconnection nsurl


    【解决方案1】:
    1. 您应该保存对您的NSURLConnection 的引用:

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

    2. 你应该启动它:

      [connection start];

    3. 您应该在didFailWithErrorconnectionDidFinishLoading 中发布它。

    【讨论】:

    • 已经 connectionDidFinishLoading() 有连接对象。在那个方法中我释放了那个。
    猜你喜欢
    • 1970-01-01
    • 2015-04-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多