1

I'm adding UIViewController in another UIView using:

self.addChild(vc)
self.view.addSubview(vc.view)
vc.didMove(toParent: self)

and removing that UIViewController by:

for childVC in self.children {
   childVC.willMove(toParent: nil)
   childVC.view.removeFromSuperview()
   childVC.removeFromParent()
}

UIViewController remove from view but deinit of that UIViewController is not calling and it does not release memory of that UIViewController.

I'm not assigning any delegate to it that might cause strong reference cycle, what other thing I've to keep in mind to release it from memory too? Any other way?

Deep Gandhi
  • 157
  • 1
  • 4
  • 13
  • 1
    Any closure that might be causing reference cycles? Or an object that holds a strong reference? – Abhinav Mathur Feb 28 '22 at 06:53
  • @AbhinavMathur Yes, one closure in UIViewController that was added in UIView, but I make it[weak self] too. It doesn't resolve issue. – Deep Gandhi Feb 28 '22 at 07:23
  • 1
    See if You have any notification observers (in any of those two view controllers) that You are not removing appropriately? – Whirlwind Feb 28 '22 at 07:27
  • @Whirlwind one of them has notification observers, observer removes code is in viewDidDisappear() method, and I checked it is called using breakpoint, but it didn't call in deinit only! – Deep Gandhi Feb 28 '22 at 07:37
  • 2
    you said you add closures - it is also important what happens inside the closure that could cause retain cycles. I suggest to add more code of anything that you think could be causing issues such as passing closures, notifications or any other properties being assigned – Shawn Frank Feb 28 '22 at 07:46
  • 1
    Debug Memory Graph should answer your question (https://developer.apple.com/documentation/xcode/gathering-information-about-memory-use)–if something holds a strong reference to your view controller, you'll see what it is. – lazarevzubov Feb 28 '22 at 07:57
  • Why nor just adding the view and then releasing its VC ? – Ptit Xav Feb 28 '22 at 09:43
  • @PtitXav it has future navigation, so cannot add view only. – Deep Gandhi Feb 28 '22 at 11:18
  • @lazarevzubov & Shawn I'll check other property as well and let you guys know how it goes – Deep Gandhi Feb 28 '22 at 11:18
  • @DeepGandhi : where are the navigation ? In the VC ? – Ptit Xav Feb 28 '22 at 11:58
  • @PtitXav yes in VC – Deep Gandhi Mar 01 '22 at 07:50
  • @Whirlwind I've doubt, don't we need to remove notification observers in deinit? – Deep Gandhi Mar 01 '22 at 07:52
  • 1
    According to [docs](https://developer.apple.com/library/archive/releasenotes/Foundation/RN-FoundationOlderNotes/index.html#10_11NotificationCenter) but I am still doing it :) – Whirlwind Mar 01 '22 at 08:06
  • @DeepGandhi , i still not understand : you want to release the VC but you need it for future navigation ? – Ptit Xav Mar 01 '22 at 08:14
  • @PtitXav, when the view loads, I need that navigation, but when when remove the superview I think it should remove that UIViewController memory, navigation is needed while it was still there not removed (ie. goto previous page). – Deep Gandhi Mar 03 '22 at 04:11

0 Answers0