我有一個對象分配和釋放的問題。 我正在研究一些具有圖像編輯功能的應用程序。 當用戶敲擊拾取圖像,然後的UIImagePickerController被呈現,則當用戶從庫拾取圖像,駁回的UIImagePickerController和然後有一個方法:「presentModalViewController:animated:」和「dismissModalViewControllerAnimated:」的正確用法是什麼?
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
//some logic
FilterViewController *filterViewController = [[FilterViewController alloc] initWithImage:imageToWorkWith
withDelegate:self];
/* present image processing screen, then release it */
[((UIViewController *)self.delegate) presentModalViewController:filterViewController animated:NO];
[filterViewController release];
}
然後出現FilterViewController - 圖像編輯屏幕。輕敲「完成」按鈕後,下一個方法將被調用:
- (void)dismissWithDone {
[self.filterViewDelegate doneImageEdittingWithImage:self.imageToWorkWithView.image];
}
它調用:
#pragma mark - FilterViewDelegate
- (void)doneImageEdittingWithImage:(UIImage *)imageToSend {
//some logic
[((UIViewController *)self.delegate) dismissModalViewControllerAnimated:NO];
}
一切似乎正常工作,但問題是,filterViewController不重新分配和堅持在記憶中。如果我再次選擇編輯一張照片,我的意思是如果創建filterViewController的方法將被再次調用,那麼先前的實例將被釋放,並且新的將被呈現,依此類推。 我認爲,當[UIViewController setChildModalViewController:]被調用時,filterViewController的前一個實例被釋放,然後,當它設置新的實例。 在增加一個釋放的情況下:
[((UIViewController *)self.delegate) presentModalViewController:filterViewController animated:NO];
[filterViewController release];
[filterViewController release];
那麼它將解散之後被釋放,但對新實例的創建將是bad_access,因爲它會嘗試的dealloc中釋放的實例。
我不明白: 1.爲什麼在關閉filterViewController之後,仍然存在大於0的引用計數,導致不會釋放它? 2.爲什麼在添加一個釋放的情況下,釋放對象的引用仍然存在於childModalViewController中?
確保您使用的代理具有assign屬性而不是retain屬性。 –
他們都是分配財產 – Nikita