Bug 3462 - Using a derived UITableView with MonoTouch.Dialog and it never get GC'd
Summary: Using a derived UITableView with MonoTouch.Dialog and it never get GC'd
Alias: None
Product: iOS
Classification: Xamarin
Component: MonoTouch.Dialog ()
Version: 5.2
Hardware: Macintosh Mac OS
: --- normal
Target Milestone: Untriaged
Assignee: Bugzilla
Depends on:
Reported: 2012-02-14 20:31 UTC by David Jade
Modified: 2018-04-17 15:07 UTC (History)
3 users (show)

Is this bug a regression?: ---
Last known good build:

Modified DemoReflectionApi.cs (4.03 KB, text/plain)
2012-02-14 20:32 UTC, David Jade

Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.

Please create a new report on Developer Community or GitHub with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:

Description David Jade 2012-02-14 20:31:07 UTC
When using MonoTouch.Dialog with a newly derived DialogViewController so that MakeTableView can be overridden to supply a customized UITableView, even with the simplest UITableView subclass the DialogViewController and UITableView are no longer finalized or GC'd. I've determined this by using tracing finalizers (that never get called) and I've seen it in the heap shot profiler too as the objects multiple.

The repo is simple. I've put together a modified version of DemoReflectionAPI.cs from the MonoTouch.Dialog Sample that demonstrates this. It presents the dialog as modal in a new UINavigationController and then it calls Dispose on the UINavigationController when it has closed to do immediate cleanup. 

When RepoDialogViewController.MakeTableView supplies a stock UITableView everything gets finalized and GC’d. If MakeTableView instead returns a derived RepoUITableView then nothing gets finalized or GC’d. You can see both cases in the attached code by flipping an #ifdef.

To build the repo case, just replace DemoReflectionAPI.cs in the MonoTouch.Dialog sample and build. Tap the "Reflection API" demo button and then close the modal view via the Done button. You can see the lack of finalizer calls in the application output or the profiler.
Comment 1 David Jade 2012-02-14 20:32:02 UTC
Created attachment 1359 [details]
Modified DemoReflectionApi.cs
Comment 2 Sebastien Pouliot 2012-02-15 09:43:38 UTC
This looks like a duplicate of #3189 which should be fixed in the next stable release.
I'll try this to see if it's the case (or not).
Comment 3 David Jade 2012-02-15 12:50:34 UTC
I don't think it is the same issue. If I change the repo code back to the original PushViewController code the RepoDialogViewController will get finalized as long as it does not return a derived UITableView. The finalization happens after a bit of a lag, usually on the next push but it does happen.

If RepoDialogViewController.MakeTableView returns a derived UITableView then the views never seem to get finalized.

I found this btw, because I've been trying to hunt down the many cases I have where pushed and presented MonoTouch.Dialogs are not getting released. I am wondering if they are all related now. I have several cases where views (or subviews) are retained for reasons I can't full trace. This is the first one I've been able to isolate. I've definitely seen the behavior described by #3189 too so that has likely been a contributing case for me as well.

I'd be happy to test if needed.
Comment 4 Sebastien Pouliot 2012-02-15 21:54:29 UTC
MonoTouch 5.2.4 is now available (and has that fix). Does it still behave the same ?
Comment 5 David Jade 2012-02-16 02:25:56 UTC
5.2.4 does not make a difference here. Whether I push a RepoDialogController or wrap it in a UINavigationController and present that as modal, if I code DialogController.MakeTableView() to return a derived UITableView then the DialogController never gets finalized (and of course neither do the MonoTouch.Dialog elements, sections, etc...)
Comment 6 PJ 2013-11-19 17:04:48 UTC
This bug has been in the NEEDINFO state with no changes for the last 90 days. Can we put this back into the NEW or CONFIRMED state, or are we still awaiting response?

If there is no change in the status of this bug over the next two weeks, this bug will be marked as NORESPONSE.
Comment 7 David Jade 2013-11-20 12:21:51 UTC
I don't know if this was on me for more info but with my test case attached above I'm not sure what else I could offer.
Comment 8 Rolf Bjarne Kvinge [MSFT] 2013-11-22 07:44:01 UTC
Info provided.
Comment 9 Rolf Bjarne Kvinge [MSFT] 2016-02-11 14:34:29 UTC
I can confirm this is still an issue, at first sight looks like there's a circular reference somewhere in MonoTouch.Dialog.
Comment 10 Alex Soto [MSFT] 2018-04-17 15:07:36 UTC

While there was[1] indeed a retain cycle on the reflection API using [OnTap] (which is fixed on [2]), DialogViewController does not have retain cycles out of the box, I wrote a sample to demonstrate this and can be found here[3].

The issue in your particular case is that you are subscribing into an event backed by an Objective-C delegate using an anonymous function which results into a retain cycle that cannot be broken unless you unsubscribe said anonymous function from the event.

So in your example you are subscribing to ViewDissapearing event of the DialogViewController

dv.ViewDissapearing += delegate {
	// some code here

But you never unsubscribe from the event and this causes the DialogViewControler to leak.

Rule of thumb when you subscribe to an event backed by an Objective-C delegate you **must always** unsubscribe.

[1]: https://github.com/migueldeicaza/MonoTouch.Dialog/pull/251
[2]: https://github.com/migueldeicaza/MonoTouch.Dialog/commit/98c2a56eaf37a483b47f12599675e41b91319d38
[3]: https://github.com/migueldeicaza/MonoTouch.Dialog/files/1918336/MonoTouch.Dialog-OnTapFix.zip