dskit icon indicating copy to clipboard operation
dskit copied to clipboard

Presenting and then dismissing leads to memory leak

Open merzal opened this issue 4 years ago • 0 comments

Describe the bug Presenting and then dismissing a DSViewController embedded to a UINavigationController (or DSNavigationViewController, does not matter) leads to memory leak.

To Reproduce I could reproduce this issue also in the DSKitExplorer project with the following steps:

  1. Added a button to ShowContentVC:
...
let button = DSButtonVM(title: "Let's do it!") { [weak self] (_: DSButtonVM)  in
    self?.presentInNavigationController(vc: ShowTopContentVC())
    //self?.present(UINavigationController(rootViewController: ShowTopContentVC()), animated: true)
}

show(content: [viewModels.list(), button.list()])
...
  1. Added button to ShowTopContentVC to be able to dismiss itself:
...
let button = DSButtonVM(title: "Go away!") { [weak self] (_: DSButtonVM)  in
    self?.dismiss()
}

show(content: [viewModels.list(), button.list()])
...
  1. Also added deinit method to ShowTopContentVC to see whether it is deallocated or not:
deinit {
    print("Deinitalizing of \(self)")
}

Expected behavior ShowTopContentVC is deallocated right after dismissing.

To ensure this happens, console should print something like this: Deinitalizing of <DSKitExplorer.ShowTopContentVC: 0x13189d200>

Additional context

  • To the 1. reproduction point: I've also tried to present ShowTopContentVC embedded into a UINavigationController instead of a DSNavigationViewController but the result is the same in both cases.
  • When presenting and dismissing a single DSViewController without embedding it into a navigation controller, then everything works as expected, the view controller is deallocated as expected.
  • I've found (very probably) the root of the problem but I don't really went deeper and don't have a fully understand how things work under the hood
    • when I comment out this line in DSBaseViewController, everything works as expected, ShowTopContentVC is deinitalized right after dismiss:
override open func viewDidLoad() {
    super.viewDidLoad()
//  DSAppearance.shared.appearancePublisher.subscribe(self)
    setUpLargeTitleDisplayMode()
    setUpProgressHud()
    setUpMessageHud()
    updateAppearance()
    showNavigationBarShadow()
}

Screenshots As you can see I've presented and dismissed ShowTopContentVC several times and all the instances are still in the memory.

Screenshot 2022-06-08 at 16 33 18

Could you please check this issue? Thank you in advance for your reply!

merzal avatar Jun 08 '22 14:06 merzal