我有一个用于连接 httprequests 的类。尽管我在连接对象的“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];}