11

My app launches with an initial view controller (lets call it as StartVC). Now when user presses a continue button, I am presenting a navigation stack (lets call it as RegisterVC) on top of StartVC. This navigation stack will contain 5 view controllers which I am pushing on it whenever user moves forward with button actions. After the 5th view controller, I am starting a new navigation stack (lets call it as LoginVC).

Now my use case is I dont want the StartVC & RegisterVC to reside in the memory as they are of no use once user has completed his registration. In order to achieve this, I am changing the AppDelegate window's root view controller to LoginVC

Below are the options which I tried on the 5th view controller of RegisterVC:

1) Changing the keywindow

    UIApplication.shared.keyWindow?.rootViewController = LoginVC
    UIApplication.shared.keyWindow?.makeKeyAndVisible()

2) Changing the window

   let appDelegate = UIApplication.shared.delegate as! AppDelegate
   appDelegate.window?.rootViewController = LoginVC
   appDelegate.window?.makeKeyAndVisible()

3) Making the previous root view controller as nil before assigning a new one.

   let appDelegate = UIApplication.shared.delegate as! AppDelegate
   appDelegate.window?.rootViewController = nil
   appDelegate.window?.rootViewController = LoginVC
   appDelegate.window?.makeKeyAndVisible()

4) I also tried the above options directly from the AppDelegate instead of doing it from the 5th view controller.

With all the above options, I tried debugging by looking at deinit on all view controllers, but none of them got deallocated. Also, I can see that 5th view controller under LoginVC in the xcode Debug View Hierarchy.

Because of not removing them from memory, the actual problem which I am facing is after presenting the LoginVC, I have a view controller whose background color alpha is less. Because of this I am seeing the RegisterVC 5th view controller underneath it.

Any help on this appreciated...

Naveen
  • 494
  • 1
  • 4
  • 18
  • 1
    I'd suggest using ["debug memory graph"](https://stackoverflow.com/questions/30992338/how-to-debug-memory-leaks-when-leaks-instrument-does-not-show-them/30993476#30993476) feature to see what is keeping strong reference to those old views controllers. – Rob Jul 16 '17 at 15:03
  • You should that answer here: http://stackoverflow.com/a/27153956/849645. If your problem is the same as mine, this fixed it for me. – CMont Aug 14 '17 at 01:33

1 Answers1

5

I think the rootViewController setting is not the problem. Perhaps you have a retain cycle in your other view controllers that stops them from being deallocated.

There are many ways you could accidentally do this (capturing strong references to self in blocks, not marking delegates or other back references as weak, etc).

You might be able to figure it out with Instruments. Here's a tutorial: http://samwize.com/2016/05/30/finding-retain-cycle-with-instruments/

Lou Franco
  • 87,846
  • 14
  • 132
  • 192
  • Or use Rob's suggestion of debug memory graph (see comment on your question) – Lou Franco Jul 16 '17 at 15:07
  • Yeah it looks like you guys are right, it seems like there is a retain cycle but I am unable to figure out where its happening... I followed the 'debug memory graph' and 'instruments' but ended no where...I am cautiously managing the 'weak self' inside closures and 'weak delegate references' but its still hiding somewhere...I will post an update once I find out where its happening...Thank you for your quick responses.. – Naveen Jul 18 '17 at 02:30
  • Hi @Venkat Any update on this? I am facing the similar issue due to which previous controller is still visible while switching the controllers. – Priyanka Wadher Mistry Jan 11 '18 at 09:51
  • No @PriyankaMistry, Its still visible and I am unable to resolve this...The only way I found this getting resolved is by dismissing it while changing the root controller. – Naveen Jan 11 '18 at 22:42
  • Hi @Naveen. Did you finally found the reason of the retain cycle? I’m facing a very similar issue and debug didn’t help me to find te root of the problem. – WedgeSparda Aug 10 '18 at 19:51
  • No @WedgeSparda, I didn't find the reason. So I resolved by dismissing it while changing the root controller (I think I used some animation so that user doesn't see it happening, not sure on this as its been long time). – Naveen Aug 11 '18 at 14:42
  • @Naveen can you paste an example of what you did, please? I think you just placed a dismiss just before the animation, or maybe on its completion block. – WedgeSparda Aug 12 '18 at 03:37
  • @Naveen Ah, don't worry, I finally made ir work thanks to your suggestion. Thank you so much. – WedgeSparda Aug 13 '18 at 06:52