我使用NSURLConnection
類來獲得在互聯網上的一些數據,我的應用程序得到在某個時刻崩潰,我得到一個錯誤表明我已經雙重釋放的對象,Shoud我釋放NSURLConnection實例變量?
以下是我創建的一個實例NSURLConnection
我如何釋放它:
NSURLRequest *request = [NSURLRequest requestWithURL:url];
NSURLConnection *connection = [[NSURLConnection alloc]initWithRequest:request delegate:self];
[connection release];
我應該釋放它時,即時通訊使用它做,對嗎?或者它在幕後發佈?
編輯
現在,我得到這個問題整理出來,只是我做的NSURLConnection
實例爲伊娃和它的dealloc釋放,這裏的代碼
頭文件:
// ....
NSURLConnection *connection;
@property (nonatomic, retain) NSURLConnection *connection;
執行文件:
// ...
NSURLRequest *request = [NSURLRequest requestWithURL:url];
self.connection = [[NSURLConnection alloc]initWithRequest:request delegate:self];
// ...
-(void)dealloc{
[connection release];
[super dealloc];
}
問題是我沒有在分配新連接之前釋放舊連接,這是通過在伊娃之前添加自己來完成的。
考慮在您的項目中使用ARC,它將有助於這樣的情況。 – 2012-02-04 22:46:34
我總是用它來釋放它裏面connectionDidFinish加載或connectionDidLoadWithError(不知道確切的名稱,但你必須有一個想法)...因爲這兩種方法都有你正在使用的當前連接的參數... – doNotCheckMyBlog 2012-02-04 23:50:51
你的意思是沒有必要如果我設法在didFinishLoading和didFinishWithErro中釋放它,可以在dealloc方法中釋放它?謝謝 – 4mahmoud 2012-02-05 14:19:17