Ustawiłem nsurl, który pobiera dane z http. po uruchomieniu instrumentu, mówi, że mam przeciek obiektu NSFNetwork.NSURLConnection leak?
i jak mogę zwolnić przycisk theConnection w (void) ButtonClicked? czy będzie później wydany?
- (void)ButtonClicked {
NSURLRequest *theRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:KmlUrl]
cachePolicy:NSURLRequestUseProtocolCachePolicy
timeoutInterval:20.0f];
NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
if (theConnection) {
// receivedData is declared as a method instance elsewhere
NSMutableData *receivedData = [[NSMutableData data] retain];
[self setKMLdata:receivedData];
} else {
// inform the user that the download could not be made
}
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
// append the new data to the receivedData
// receivedData is declared as a method instance elsewhere
[KMLdata appendData:data];
NSLog(@"didReceiveData");
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
// release the connection, and the data object
[connection release];
[KMLdata release];
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
// release the connection, and the data object
[connection release];
// receivedData is declared as a method instance elsewhere
[KMLdata release];
}
Podczas rozmowy autorelease będzie działać, to nie w przypadkach, gdy pełnomocnik jest uwalniane, zanim połączenie zostanie zwolniony. Lepszym sposobem byłoby przypisanie połączenia do zmiennej instancji i zwolnienie + nil go, gdy nie jest potrzebne. Jeśli połączenie ivar jest nadal przypisane, gdy obiekt deallocs, delegat musi być ustawiony na zero przed zwolnieniem połączenia. – rpetrich
@rpetrich Zgadzam się, że Twoja sugestia jest bardziej wyczerpująca, ale było jasne, że jego pytanie wykazało podstawowe nieporozumienie dotyczące zarządzania pamięcią na iPhonie i dlatego chciałem dać mu koncepcyjnie łatwiejsze rozwiązanie – h4xxr
@rpetrich, czy możesz podać przykład kodu. Próbowałem rozwiązać ten problem na wiele sposobów i nadal mam wyciek. – Jordan