// Create the request.
NSURLRequest *theRequest=[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.apple.com/"]
cachePolicy:NSURLRequestUseProtocolCachePolicy
timeoutInterval:60.0];
// create the connection with the request
// and start loading the data
NSURLConnection *theConnection=[[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
if (theConnection) {
// Create the NSMutableData to hold the received data.
// receivedData is an instance variable declared elsewhere.
receivedData = [[NSMutableData data] retain];
} else {
// Inform the user that the connection failed.
}
既然我們不通過調用retain來擁有receivedData,我們不是在泄漏內存嗎?Apple NSURLConnection文檔是否錯誤?
什麼時候你應該釋放連接並收到數據?
你在哪裏發佈? – 2010-08-20 22:30:07
完成加載後釋放連接。無論是在連接完成加載或連接確實失敗,錯誤。完成後釋放數據。只有你可以知道你什麼時候做完了。基本的內存管理原則。 – Jasarien 2010-08-20 23:32:40