http에서 데이터를 가져 오는 nsurl을 설정했습니다. 장비를 실행할 때 누출 NSFNetwork 개체가 있다고합니다.NSURLConnection 누출?
및 어떻게 연결을 해제합니까 (무효) Button30ed에? 또는 나중에 출시 될 예정입니까?
- (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];
}
, 그 대리자가 연결이 해제 이전에 출시 된 경우에 실패합니다. 더 좋은 방법은 연결을 인스턴스 변수에 할당하고 필요하지 않을 때 release + nil을하는 것입니다. 객체가 dealloc 될 때 연결 ivar이 여전히 할당되어 있으면 연결을 해제하기 전에 델리게이트를 nil로 설정해야합니다. – rpetrich
@ rfpetrich 나는 귀하의 제안이 더 포괄적이라는 데 동의하지만 그의 질문이 iPhone에서의 메모리 관리에 대한 기본적인 오해를 보여 주었기 때문에 개념적으로 더 쉬운 해결책을주고 싶었습니다. – h4xxr
@rpetrich, 코드 예제를 제공 할 수 있습니까? 나는 여러 가지 방법으로이 문제를 해결하려고 노력했지만 누수가 여전히있다. – Jordan