Yan*_*aim 2 iphone memory-leaks objective-c httprequest 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];}
Run Code Online (Sandbox Code Playgroud)
静态分析器会将此称为泄漏,因为您无法保证release实际调用具有某个特征的方法.
如果设置receivedData为保留属性,则执行此操作
self.receivedData = [NSMutableData data];
Run Code Online (Sandbox Code Playgroud)
然后在你的dealloc(以及你的didFail和didFinish,而不是发布):
self.receivedData = nil;
Run Code Online (Sandbox Code Playgroud)
你会没事的.
正如jbat100指出的那样,如果是!connectionResponse,你也会泄漏url和urlRequest,除非你从问题中省略了这段代码
| 归档时间: |
|
| 查看次数: |
2428 次 |
| 最近记录: |