Bugzilla – Bug 1889
UINavigationController leaks view controllers
Last modified: 2014-04-21 06:16:33 EDT
Created attachment 827 [details]
zipped test project
See attached test case (comment out the call to FixCase3 (which is this bug) in
FinishedLaunching to repro). Run the app, then tap twice on the back button.
Notice how only one view[controller] is freed in the application output.
Basically it pushes view controllers in code, but when the user pops them in
the ui the popped view controllers aren't freed. This is because
UINavigationController fetches the ViewControllers property every time the
property changes through managed code, but in this case it's changed through
native code, so the managed array isn't updated and we end up with the popped
viewcontroller still reachable by the gc.
*** Bug 1840 has been marked as a duplicate of this bug. ***
*** Bug 5181 has been marked as a duplicate of this bug. ***
I can repro this with MonoTouch 5.3.3.
Is there a timeframe for a fix, or perhaps a workaround? Rolf, I tried reading
the ViewControllers.Length property of my UINavigationController by putting
this line of code in the ViewDidAppear() override in the first UIViewController
that gets pushed onto my nav stack... with the intention that it will cause any
UIViewControllers that were pushed and subsequently popped to get GC'ed.
int count = this.NavigationController.ViewControllers.Length;
No dice - HeapShot still shows those UIViewControllers as live.
I have not tried a timer on a separate thread like in the project you enclosed
- I sure hope there's a more straightforward workaround (or a fix in the
Omri: for now I don't know about any workarounds besides the timer one.
In any case this is unlikely to cause any real-world problems (the view
controller will just be freed later when you push another one).
What happens is that the child MyView keeps a reference to the
The MyView itself is added to the MyViewController as its View, which creates
two references: one from the managed MYView to the unmanaged MyView. The
second comes from the GCHandle one that we create when the
MyViewController.View is assigned.
So we have an unmanaged object keeping our MyView alive, which keeps
MyViewController alive, which in turn has a reference to the MyView.
So this is a cycle.
The workaround is to not have the MyView reference the MyViewController.
Miguel: that is not correct. This still leaks:
The issue is that we keep a stack of pushed view controllers, but we're not
notified when the view controllers are popped, so they stay in that stack until
either a new view controller is pushed or popped (programmatically) or the user
fetches the ViewControllers array.
Unfortunately I haven't found a way to be notified when the user pops a view
controller, so I'm not sure this is even fixable by any reasonable means.
We've run into similar issue - especially visible when me modally present
Currently we have to do the hack like
to make our presented controllers gc'able.
Maybe you could re-read the cached properties when the gc is going? This way no
obsolete references would be held.
We are also hitting this, and it is similar to this bug:
A workaround is to use the DidShowViewController method of the Delegate like
public void DidShowViewController(UINavigationController
navigationController, UIViewController viewController, bool animated)
// Force the __mt_ViewControllers_var field to be updated.
var vc = navigationController.ViewControllers;
Needless to say, that's a stupid thing to have to do every time you use a
This is the second similar leaky behavior I have found today related to these
private cache fields. I don't fully understand what they are for (is this how
we're keeping C# objects in memory? I thought the runtime had a smarter
mechanism for that), but it seems destined to cause leaks. Avoiding leaks in
Xamarin is a black art at this point because of crazy little behaviors like
We agree, this is a limitation of the current design.
We have addressed this, and the upcoming beta will include a new memory
management system that avoids this problem once and for all. It is only a few
Hooray! Will it work like Xamarin.Android's trick with the weak reference swap
It is a different system, it is inspired by a lot of the work on "SGen/Enable
New Ref Count".
We will post more details as we get close to the release.
This issue is resolved when using the New-Refcount (NRC) feature . This is
not a new feature  but it has totally revamped  in the last two months
based on the bug reports, like this one.
With XI 7.2.1 this feature works with both Boehm (default) and Sgen garbage
collectors. NRC has also been enhanced and, right now, there are no known
issues (bugs) against it.
While NRC is not the default option (in 7.2.1) we plan to make it so in the
near future. Additional testing and feedback on the feature would be
Also note that while the test case still expose leaks (MyView[Controller] 2 and
4), those are due to circular references crossing the native/managed boundary,
which is not what this bug report is about.
 Newer versions of Xamarin Studio will remove the experimental tag on the
feature when XI 7.2.1+ is used.
Great news if this is fixed. Any idea what release it will be in?
I mean when the release with it in all be
@BB: it's in the 7.2.1 release, which is currently in beta.