NSMutableData 的内存泄漏
我有一个用于连接 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
静态分析器会将其称为泄漏,因为您不能保证具有
release
功能的任何一个方法实际上都会被调用。如果您将receivedData设置为保留属性,并
在dealloc中执行Then(还有didFail和didFinish,而不是释放):
您会没事的。
正如 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 doThen in your dealloc (and also your didFail and didFinish, instead of the release):
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
您需要真正确保这两个委托方法是完成请求的唯一可能方式。我可以在这里看到泄漏,
您没有执行
当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
you do not do the release operations
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.
如果您想删除此泄漏,请在.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
你为什么认为你在泄漏? (
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.