Bug 35100 - Dispose(true) is not called on controller with segue in UINavigationController
Summary: Dispose(true) is not called on controller with segue in UINavigationController
Alias: None
Product: iOS
Classification: Xamarin
Component: General ()
Version: XI 9.0 (iOS9)
Hardware: PC Mac OS
: --- normal
Target Milestone: Untriaged
Assignee: Bugzilla
Depends on:
Reported: 2015-10-20 11:40 UTC by John Miller [MSFT]
Modified: 2015-12-03 14:49 UTC (History)
3 users (show)

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

Sample Project (22.59 KB, application/zip)
2015-10-20 11:40 UTC, John Miller [MSFT]

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 John Miller [MSFT] 2015-10-20 11:40:52 UTC
Created attachment 13455 [details]
Sample Project


   Dispose(true) is not called when a controller is popped from a UINavigationController (pressing back button). 

**Steps to Reproduce:**

   1. Run the attached sample on an iOS device. 
   2. Press the Next View button to show the SecondViewController
   3. Press the Back button
   4. Repeat 2-3

**Actual Results:**

   Dispose(false) is called, and you will see the log messages (in Debug mode) indicating that. 
   Dispose(true) is not called on SecondViewController when the Back button is pressed. 

**Expected Results:**

   Expecting Dispose(true) to be called at some point when Back is pressed because SecondViewController is not needed anymore. 

**Build Date & Platform:**

=== Xamarin Studio ===

Version 5.9.7 (build 22)
Installation UUID: e01c3049-a2d2-4e0a-aad8-afe6fb627c4d
	Mono 4.0.4 ((detached/d481017)
	GTK+ 2.24.23 (Raleigh theme)

	Package version: 400040004

=== Xamarin.Android ===

Version: (Business Edition)
Android SDK: /Users/johnmiller/Library/Developer/Xamarin/android-sdk-macosx
	Supported Android versions:
		2.3   (API level 10)
		4.0.3 (API level 15)
		4.1   (API level 16)
		4.2   (API level 17)
		4.4   (API level 19)
		5.0   (API level 21)
		5.1   (API level 22)
Java SDK: /usr
java version "1.8.0_25"
Java(TM) SE Runtime Environment (build 1.8.0_25-b17)
Java HotSpot(TM) 64-Bit Server VM (build 25.25-b02, mixed mode)

=== Xamarin Android Player ===

Version: Unknown version
Location: /Applications/Xamarin Android Player.app

=== Apple Developer Tools ===

Xcode 7.0 (8227)
Build 7A220

=== Xamarin.iOS ===

Version: (Business Edition)
Hash: 1d27ac2
Branch: master
Build date: 2015-09-25 18:08:44-0400

=== Xamarin.Mac ===

Version: (Business Edition)

=== Build Information ===

Release ID: 509070022
Git revision: 6bd1f169df44ca96addf8a035316c535a4fa46fa
Build date: 2015-09-30 12:30:15-04
Xamarin addins: 1c3e5c0859bdfec0ecd481a57ad6c03bc22f5536

=== Operating System ===

Mac OS X 10.10.5

**Additional Information:**

   Looking for an explanation of how this is designed to work, and if the Expected results are reasonable or misunderstood.
Comment 1 Rolf Bjarne Kvinge [MSFT] 2015-12-03 14:49:58 UTC
The behavior is as expected.

Dispose is called when the object is garbage collected, which only happens the the garbage collector is executed. You can confirm this by manually calling GC.Collect, and Dispose will be called soon after.

In the simulator Xamarin.iOS will force a garbage collection frequently to catch a certain type of programming errors faster (it's better that bugs show up when the developer debugs in the simulator than when the user runs the app on his device) - this is why Dispose only seems to be called in the simulatur (it will be called on device too, but only when the garbage collector decides to collect, which may take a while).

Also Xamarin.iOS will never call Dispose directly, it's called from the finalizer, which means 'disposing' will always be false (if you call Dispose directly yourself, you'll get 'disposing = true').