3

I am designing a game with 6 view controllers.

I use modal segues to navigate forward and unwind segues to navigate back.

The game flow through the view controllers is A -> B -> C -> D -> E -> F.

When I try to unwind from F to A dealloc is only called on B and C. This means that each time the game is played the memory used is constantly increasing.

I initially thought the problem was due to something being retained causing dealloc not to be called on view controllers D and E. However, if I set the unwind segue to go from F to D dealloc is called for both F and E which would seem to rule out this as the problem.

I also have a route directly to from view controller A to F. When I unwind in this case dealloc is called on F correctly.

View controllers F and C call the same unwind action on A. I assume this isn't the cause as I have tried changing it to use unique unwind actions for each view controller.

Any thoughts on what is causing this? I have copied some of the code used below. Please let me know what other code (if any) would be helpful in resolving this. Any help would be greatly appreciated.

Unwind action as below:

- (IBAction)unwindToMainMenu:(UIStoryboardSegue *)unwindSegue
{}

Call to trigger unwind segue from view controller F to A:

[self performSegueWithIdentifier:@"ReturnToMainMenu" sender:self];
  • Have you tried using the "Leaks" instrument, which can point out retain cycles and give you a history of all retains/releases for any object? – Jesse Rusak Jul 05 '14 at 21:46
  • I have tried leaks instrument but I am not too familiar with it. It pointed me to PrepareForSegue being where the issue was but I'm not sure how to interpret it. I am fairly new to iOS development :) – user1980555 Jul 05 '14 at 21:50
  • Well, there's no way we can help with the information you've provided, so I would either suggest watching some WWDC videos on how to use instruments, or else create a minimal sample project that shows the problem you're seeing so we can help. – Jesse Rusak Jul 05 '14 at 21:51
  • What information would help? I can provide more code, just didn't want to throw all my code at the page :) – user1980555 Jul 05 '14 at 21:57
  • You should try to create a minimal example: http://stackoverflow.com/help/mcve – Jesse Rusak Jul 05 '14 at 21:58
  • I will have a go at this though as I don't know where the issue is it could be difficult to replicate with minimal code. – user1980555 Jul 05 '14 at 22:01
  • From my past knowledge, there are a few things that can prevent a view controller from deallocing properly. They are, cyclic references (A strongly reference B and vice versa, use weak for either A or B to fix this), NSTimers (ensure you invalidate your timers and set to nil before dealloc) and Delegates (set delegates to nil). – Zhang Jul 07 '14 at 09:14

1 Answers1

1

Following your description, the problem should be on D. Probably D is retained from someone else and so it is not deallocated when you go from F directly to A. Being not deallocated it continue to retains E which retains F.

So, be concentrate on D ;)

Matteo Gobbi
  • 17,697
  • 3
  • 27
  • 41
  • Any idea how I narrow down where the issue is? I have been stuck on this for a couple of days now. I pretty much removed all of my code to try to locate the problem but it still existed. – user1980555 Jul 05 '14 at 21:53
  • What could cause a retain issue? I have tried the analyse tool and leaks and there is no visible leak on them. – user1980555 Jul 05 '14 at 21:54
  • Are you using external library to do something? I had something to similar some week ago..at the end the problem was in a third party library...replacing it, everything was good. – Matteo Gobbi Jul 05 '14 at 21:54
  • The only 3rd party I use is Google Admob but I have the latest version. – user1980555 Jul 05 '14 at 21:56
  • Thanks for your help. I have located the problem in view controller D as you suggested and it is now fixed. The problem was that I had set [self becomeFirstResponder] but had no call to [self resignFirstResponder] adding this to prepareForSegue resolved the problem. – user1980555 Jul 08 '14 at 10:43