Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are there scenarios where dealloc will get called without viewWillDisappear not being called?

In a controller we have added observer in viewDidAppear and it's removed in viewWillDisappear. There is no observer in init/viewDidLoad.

In such a case the below line for safety purpose/is it required in dealloc method?

[[NSNotificationCenter defaultCenter] removeObserver:self];

Now the question/doubt is are there scenarios where dealloc will get called without viewWillDisappear not being called? What about when memory warnings are called. What happens in those cases?

Thanks.

like image 674
selva Avatar asked Oct 01 '22 22:10

selva


1 Answers

One scenario is:

  1. VC1 implements the class HelperVC, it's delegates and added HelperVC as it's subview.
  2. Close button on the HelperVC calls closeAll delegate method, which is implemented in VC1.
  3. The closeAll method in VC1 sets the HelperVC object to nil.
  4. Now dealloc method is called in HelperVC instead of viewWillDisappear. Because we didn’t remove the HelperVC view, we have made HelperVC object to nil.
like image 105
Manoj Avatar answered Oct 06 '22 00:10

Manoj