內存泄漏我有一個與HTTPRequests的連接類。我得到了內存泄漏的「NSMutableData」本書雖然是我在「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];}
感謝您的回答。我試過你的解決方案,內存泄漏仍在這裏。 (我在.h文件中保留了receicvedData對象,我在dealloc中釋放了它,並在連接失敗/成功時執行了'self.receivedData = nil;'我也初始化了它,如您所說:'self.receivedData = [NSMutableData data];順便說一句 - 我的測試是在xcode文件中的 –
您是否正在使用receivedData的任何其他地方,您是否也可以顯示該代碼? – jrturton
我在連接方法中使用它:'[receivedData setLength: 0];''和'[receivedData appendData:data];' –