Destroying UIPopoverController object


#1

In ItemsViewController.m we destroy UIPopoverController object explicitly by making imagePopover point to nil.
imagePopover=nil;
My question is where is the need to do this. When ItemsViewController object will destruct so will all of its ivars. When instance pointer vars like imagePopover destroy, then automatically the UIPopoverController shall lose ownership. It should become ownerless. And hence iOS will purge the chunk of memory that is allocated for it( the object). So why let it not happen on its own?


#2

You are correct that the popover would be destroyed when the view controller is destroyed, but here we are just managing our memory better. When the popover is dismissed, we no longer need it, so we clean up the memory associated with it.


#3

But we know that ivars live alongside their respective objects in the heap memory section so as soon as a destruction chain is triggered with the object being destroyed first followed by all it’s ivars, where could there be a possibility for a memory leak? Coz when imagePopover gets destroyed there is no way one can reuse it. . I’m a bit confused on this.


#4

It’s never a memory leak with this code. We are just saying that we are done with the popover, so we might as well destroy it to return memory back to the file system. Not setting the popover ivar to nil will work just fine, as you’ve deduced, it just means our application is using more memory that it needs to be using.