UISplitViewController on Compact Width leaks first DetailViewController

I'm trying to use UISplitViewController for an iPhone + iPad App.

The Master is a Menu, when one entry is selected, the Detail shows a ViewController for the selected entry.

On iPad, this works as expected.

On iPhone however (and also on iPad in compact width), the first DetailViewController leaks ans is never deallocated.

I've confirmed with the memory graph instrument that on iPad / Regular Width, there is always only 1 instance of DetailViewController alive, but on iPhone / Compact Width there are always 2 instances of DetailViewController alive.

I have put together a minimal sample project that shows this issue here https://github.com/iv-mexx/uisplitviewcontroller-memoryleak
This is a known problem. UISplitViewController internally keeps a reference to a detail (a.k.a. secondary) view controller. When it expands (when transitioning from compact to regular width classes), if it cannot find a new secondary view controller by other means, it will use that internal reference as a fallback.

Unfortunately, this is often more confusing than helpful.

You can use the view controller's viewWillDisappear method as a signal to release any resources it might be using. You'll also find that, if you follow the chain of parentViewControllers upwards, it is not in the window's rootViewController. Also, its splitViewController method returns nil.

With UISplitViewControllers created with the iOS 14 API, it may be possible to eliminate this internal reference. No promises, but please try later iOS 14 betas (after beta 1) for updates.
Thanks for the Answer. I can see now in which case this is necessary - when changing from Regular Width to Compact and then back to Regular when there is no Detail Pushed while in Compact.

However, the details of this are surprising to me. It seems like it saves the Detail ViewController at the first time the Master is show. I would have expected it to e.g. keep the last Detail ViewController as fallback.

Anyway, I will make sure to cleanup everything in viewWillDisappear.
UISplitViewController on Compact Width leaks first DetailViewController
 
 
Q