-1

I'm using this code to reload root view controller of navigation controller.

if let newVC  = self?.storyboard?.instantiateViewController(withIdentifier: "MyViewController"), let nc = self?.navigationController {
                nc.setViewControllers([newVC], animated: true)
            }

But I've found that this code yeilds memory leak: memory is not released using this code.

How can catch and fix this? Any suggestions?

Vyacheslav
  • 26,359
  • 19
  • 112
  • 194
  • How are you telling that there is a leak? and what leaks? – Alistra Apr 19 '17 at 10:35
  • how can i release memory of previous root view controller? @Alistra – Vyacheslav Apr 19 '17 at 10:41
  • it is automatically released if it doesn't have any strong references. – ankit Apr 19 '17 at 10:46
  • write down "deinit{print(self)}" in that VC class and see whether it is called or not. – ankit Apr 19 '17 at 10:48
  • @ankit thanks. i will try – Vyacheslav Apr 19 '17 at 11:02
  • wc @Vyacheslav. – ankit Apr 19 '17 at 11:04
  • 1
    If you run the app in the debugger, reproduce the problem, and then tap on the "debug memory graph", you can see which objects are still around and, more importantly, what precisely is maintaining the strong reference to them. If you're leaking, I suspect the code above is not the source of the problem, but rather some other strong reference cycle or something like repeating timers that are keeping a strong reference. See http://stackoverflow.com/questions/30992338/how-to-debug-memory-leaks-when-leaks-instrument-does-not-show-them/30993476#30993476 – Rob Apr 19 '17 at 12:05
  • @CMont yes I think it is something about that – Vyacheslav Aug 13 '17 at 15:28
  • Try the answer posted here instead : http://stackoverflow.com/a/27153956/849645. If your problem is the same as mine, it fixed my problem. – CMont Aug 14 '17 at 01:34

1 Answers1

0

When you set new view controllers entire contents of stack get replaced by new viewControllers. All viewControllers get pop.

You can check this by writing deinit methods of all viewControllers that are get replaced. all deinit methods get called when you replace viewControllers. It automatically release memory, you don't have to explicitly handle memory or release memory.

Check this link: https://developer.apple.com/reference/uikit/uinavigationcontroller/1621861-setviewcontrollers?language=objc

AkBombe
  • 638
  • 4
  • 10