NSMutableData 的内存泄漏

发布于 2024-12-08 18:50:57 字数 1471 浏览 0 评论 0原文

我有一个用于连接 httprequests 的类。我收到“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];}

I have a class for connecting with httprequests. I am getting a memory leak for "NSMutableData" altho I am releasing it in "didFailWithError" and in "connectionDidFinishLoading" of the connection object:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

独自唱情﹋歌 2024-12-15 18:50:57

静态分析器会将其称为泄漏,因为您不能保证具有 release 功能的任何一个方法实际上都会被调用。

如果您将receivedData设置为保留属性,并

self.receivedData = [NSMutableData data];

在dealloc中执行Then(还有didFail和didFinish,而不是释放):

self.receivedData = nil;

您会没事的。

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

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.

If you set receivedData as a retained property, and do

self.receivedData = [NSMutableData data];

Then in your dealloc (and also your didFail and didFinish, instead of the release):

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

痴骨ら 2024-12-15 18:50:57

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

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

您没有执行

[url release];
[urlRequest release];

当connectionResponse 为非零时执行的释放操作。另一方面,我强烈建议使用 ASIHTTP Obj C 库来完成此类工作。

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. On another note I strongly suggest the ASIHTTP Obj C library for doing this type of stuff.

西瑶 2024-12-15 18:50:57

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

if you want remove this leak take NSURLConnection in .h file and release that in connectionDidFinishLoading method .reason is you are allocted NSURLConnection object there but you cann't release over there if release app kill over there .that why you have to create NSURLConnection object in .h

简单爱 2024-12-15 18:50:57

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

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

Why do you think you are leaking? (NSMutableData) If it is because of Xcode's Analyze option; 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文