【问题标题】:memory leak with NSMutableDataNSMutableData 的内存泄漏
【发布时间】:2011-10-05 10:10:17
【问题描述】:

我有一个用于连接 httprequests 的类。尽管我在连接对象的“didFailWithError”和“connectionDidFinishLoading”中释放了“NSMutableData”的内存泄漏:

- (BOOL)startRequestForURL:(NSURL*)url {

[url retain];


NSMutableURLRequest* urlRequest = [[NSMutableURLRequest alloc] initWithURL:url];
// cache & policy stuff here
[[NSURLCache sharedURLCache] removeAllCachedResponses];
[urlRequest setHTTPMethod:@"POST"];
[urlRequest setHTTPShouldHandleCookies:YES];
NSURLConnection* connectionResponse = [[[NSURLConnection alloc] initWithRequest:urlRequest delegate:self] autorelease];
if (!connectionResponse)
{
    // handle error
    return NO;
} else {
    receivedData = [[NSMutableData data] retain]; // memory leak here!!!
}

[url release];

[urlRequest release];


return YES;}

- (void)connection:(NSURLConnection*)connection didFailWithError:(NSError*)error {
UIAlertView *alert =
[[[UIAlertView alloc]
  initWithTitle:NSLocalizedString(@"Connection problem", nil)
  message:NSLocalizedString(@"A connection problem detected. Please check your internet connection and try again.",nil)

  delegate:self
  cancelButtonTitle:NSLocalizedString(@"OK", nil)
  otherButtonTitles:nil, nil]
 autorelease];
[alert show];

[connectionDelegate performSelector:failedAction withObject:error];
[receivedData release];}

- (void)connectionDidFinishLoading:(NSURLConnection*)connection {
[connectionDelegate performSelector:succeededAction withObject:receivedData];
[receivedData release];}

【问题讨论】:

    标签: iphone objective-c memory-leaks httprequest nsmutabledata


    【解决方案1】:

    静态分析器会将此称为泄漏,因为您不能保证实际调用具有release 的任何方法。

    如果您将receivedData 设置为保留属性,则这样做

    self.receivedData = [NSMutableData data];
    

    然后在你的 dealloc 中(还有你的 didFail 和 didFinish,而不是 release):

    self.receivedData = nil;
    

    你会没事的。

    正如 jbat100 所指出的,如果 !connectionResponse,您也会泄漏 url 和 urlRequest,除非您从问题中省略了此代码

    【讨论】:

    • 感谢您的回答。我尝试了您的解决方案,但内存泄漏仍然存在。 (我在.h文件中有receicvedData对象并保留。我在dealloc中释放了它,在连接失败/成功中我做了self.receivedData = nil;。我还按照你说的对其进行了初始化:self.receivedData = [NSMutableData data];。顺便说一句-我的测试在 xcode 工具中。
    • 您是否在其他任何地方使用 receivedData 做任何事情,您也可以显示该代码吗?
    • 我在连接方式中使用它:[receivedData setLength:0];[receivedData appendData:data];
    • 我的想法已经用完了,抱歉 :(
    【解决方案2】:

    您需要确保这两个委托方法是完成请求的唯一可能方式。我可以在这里看到泄漏

    if (!connectionResponse)
    {
        // handle error
        return NO;
    }
    

    你不做发布操作

    [url release];
    [urlRequest release];
    

    当connectionResponse 为非nil 时你会做什么。另一方面,我强烈建议使用 ASIHTTP Obj C 库来做这类事情。

    【讨论】:

      【解决方案3】:

      如果您想删除此泄漏,请在 .h 文件中获取 NSURLConnection 并在 connectionDidFinishLoading 方法中释放它。原因是您在那里被分配了 NSURLConnection 对象,但如果释放应用程序杀死那里,您不能在那里释放。这就是为什么您必须在 .h 中创建 NSURLConnection 对象

      【讨论】:

      • NSURLConnection 是自动释放的,这不是问题。
      【解决方案4】:

      为什么你认为你在泄漏? (NSMutableData) 如果是因为 Xcode 的分析选项;好吧,它说谎,因为它甚至无法处理如此明显的复杂情况。

      但是,正如 Narayana 指出的那样,您也泄漏了连接,您应该在完成和失败委托方法中释放它。

      【讨论】:

        猜你喜欢
        • 2018-01-14
        • 1970-01-01
        • 2012-07-06
        • 1970-01-01
        • 1970-01-01
        • 2019-07-05
        • 2010-12-17
        • 2016-05-09
        • 2017-11-03
        相关资源
        最近更新 更多