Delegate of UIImageViewController is nil when didReceiveMemoryWarning is received

385 Views Asked by At

I am trying to figure out why when my application gets a didReceiveMemoryWarning while the UIImagePickerController has control, the picker (and thus the delegate of the picker) is set to the deallocated viewcontroller?

View Controller one calls

self.postImagePicker = [[UIImagePickerController alloc] init];
self.postImagePicker.delegate = self;
self.postImagePicker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
self.postImagePicker.allowsEditing = YES;
[self presentModalViewController:self.postImagePicker animated:YES];

Next, the UIImageViewController is displayed and an image is selected. Then simulate a memory warning. The view controller that presented the dialog gets a memory warning error and the picker is set to 0x0 (which means that the delegate is also now invalid).

I'm using ARC. Has something changed in the way that an UIImageViewController is called so that the view that presented it is not reloaded thus causing the pointer to the UIImageViewController to become invalid?

2

There are 2 best solutions below

0
On BEST ANSWER

Check didReceiveMemoryWarning. Do you have self.postImagePicker = nil; there? Check the didReceiveMemoryWarning methods for your upstream view controllers. Do they have a reference to this view controller? If not, the navigation controller may be dumping this view controller. delegate is a weak reference, so it's not enough to keep your view controller around if nothing else is.

0
On

I'm going to guess that you nil'ed your view controller in some other view controller's viewDidUnload. There is no reason for your view controller to be deallocated unless it's explicitly released (or you didn't keep a strong reference to it in the first place).