简体   繁体   English

NSMutableData导致内存泄漏

[英]memory leak with NSMutableData

I have a class for connecting with httprequests. 我有一个用于连接httprequests的类。 I am getting a memory leak for "NSMutableData" altho I am releasing it in "didFailWithError" and in "connectionDidFinishLoading" of the connection object: 我正在获取“NSMutableData”的内存泄漏,我将在“didFailWithError”和连接对象的“connectionDidFinishLoading”中释放它:

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

The static analyser will call this a leak because you are not guaranteeing that either of the methods featuring a release will actually be called. 静态分析器会将此称为泄漏,因为您无法保证实际调用具有release任何方法。

If you set receivedData as a retained property, and do 如果将receivedData设置为保留属性,则执行此操作

self.receivedData = [NSMutableData data];

Then in your dealloc (and also your didFail and didFinish, instead of the release): 然后在你的dealloc(以及你的didFail和didFinish,而不是发布):

self.receivedData = nil;

You will be OK. 你会没事的。

As jbat100 points out, you are also leaking url and urlRequest if the !connectionResponse, unless you have omitted this code from the question 正如jbat100所指出的那样,如果是!connectionResponse,你也会泄漏url和urlRequest,除非你从问题中省略了这段代码

You need to make really sure that these two delegate methods are the only possible way the request could finish. 您需要确保这两个委托方法是请求完成的唯一可能方式。 I can see a leak here 我可以在这看到泄漏

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

you do not do the release operations 你不做发布操作

[url release];
[urlRequest release];

Which you do when the connectionResponse is non-nil. 当connectionResponse为非零时你会这样做。 On another note I strongly suggest the ASIHTTP Obj C library for doing this type of stuff. 另一方面,我强烈建议使用ASIHTTP Obj C库来完成这类工作。

如果你想删除这个泄漏,请在.h文件中使用NSURLConnection并在connectionDidFinishLoading方法中释放.reason是你在那里分配了NSURLConnection对象但是你不能在那里释放如果释放app杀死那里。这就是为什么你必须创建NSURLConnection对象在.h

Why do you think you are leaking? 为什么你认为你在泄漏? ( NSMutableData ) If it is because of Xcode's Analyze option; NSMutableData )如果是因为Xcode的Analyze选项; well, it lies, as it can't handle even such obvious complex situations. 好吧,它是谎言,因为它甚至无法处理这种明显复杂的情况。

However, as Narayana pointed out, you are also leaking the connection, which you should release in both the finish and fail delegate methods. 但是,正如Narayana指出的那样,您也在泄漏连接,您应该在完成和失败委托方法中释放连接。

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

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