Home Ask Login Register

Developers Planet

Your answer is one click away!

Randall February 2016

dismissViewControllerAnimated sends message to zombie object

I have two UIViewControllers, VC1 and VC2. VC1 has a button that invokes a triggered Modal segue to VC2. This segue is defined in the storyboard. In VC2, the user can return to VC1 using a pan gesture that executes this line of code:

[self.presentingViewController dismissViewControllerAnimated:YES completion:nil];

This code has worked for a long time. However, all of a sudden when this line runs, the app crashes. I enabled zombie objects and I can see this error:

-[VC2 retain]: message sent to deallocated instance 0x7f843a81e200

I've run the zombie profiler and here are the results:

enter image description here

UIClassSwapper initWithCoder seems to be where the app crashes, and there appears to be a an over retained object there, but I can't get any further. When I set an exception breakpoint, I just get into the assembly code, and this isn't of any apparent help.

So, there are a couple of questions here. The first is simply what am I doing wrong in how I am presenting and dismissing this view controller? After that, I don't know how to crack into the event history to figure out how to handle this zombie object.

Any help is appreciated. Thank you!


ETech February 2016

It seems that you use ARC project. Please provide more code:
1. How you initialize presentingViewController property (and declare). Is it (nonatomic,strong)?
2. How do you put this ViewController to the screen. Is it a kind of modal view controller etc?

-- added after comment.
ARC based project decides automatically if the object is needed or not and removes it from the stack automatically. So, the only one useful recipe to avoid this problem is to add NSNotification observer and post there notifications time after time. It will keep view controller in the stack and avoid automatic release.
Another solution - check if the controller is still in the stack:

    if (!self.presentingViewController) [[THECONTROLLER alloc] init];

The third one - is to use some object to declare VC1 and VC2 as class variables, and present them as modal view controller when needed. In this case, object and its variables will be kept untouched as long as any of VCs presented.

In any case, the Exception happens because ARC considers that VC is not needed anymore and releases it atomatically.

Post Status

Asked in February 2016
Viewed 2,776 times
Voted 5
Answered 1 times


Leave an answer

Quote of the day: live life