Bug 34450 - Finalizers aren't executed if Java.Lang.Object instance re-registers for finalization
Summary: Finalizers aren't executed if Java.Lang.Object instance re-registers for fina...
Alias: None
Product: Android
Classification: Xamarin
Component: BCL Class Libraries ()
Version: 4.20.0
Hardware: PC Mac OS
: --- normal
Target Milestone: ---
Assignee: Mark Probst
Depends on:
Reported: 2015-09-30 12:28 UTC by Jonathan Pryor
Modified: 2016-01-15 14:23 UTC (History)
2 users (show)

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

Scratch.Finalizers.zip (29.05 KB, application/zip)
2015-09-30 12:28 UTC, Jonathan Pryor

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 for Bug 34450 on Developer Community or GitHub if you have new information to add and do not yet see a matching new report.

If the latest results still closely match this report, you can use the original description:

  • Export the original title and description: Developer Community HTML or GitHub Markdown
  • Copy the title and description into the new report. Adjust them to be up-to-date if needed.
  • Add your new information.

In special cases on GitHub you might also want the comments: GitHub Markdown with public comments

Related Links:

Description Jonathan Pryor 2015-09-30 12:28:03 UTC
Created attachment 13145 [details]

Consider the following class:

	class MyClass : Java.Lang.Object {

		~MyClass ()
			Console.WriteLine ("# jonp: MyClass.Finalize()");
			throw new Exception ("DIE DIE DIE");

		protected override void Dispose (bool disposing)
			base.Dispose (disposing);

			GC.ReRegisterForFinalize (this);

Note that it (1) overrides Java.Lang.Object.Dispose(), and (2) calls GC.ReRegisterForFinalize().

If we create and dispose an instance of this class:

	var c = new MyClass();

The intuitive expectation is that the finalizer will execute, which in turn will cause the process to exit (as the finalizer throws an exception).

What *actually* happens is *nothing*: the finalizer never executes, and the process keeps running.

I suspect that this has something to do with the GC bridge, in that if I do the same thing *without* inheriting Java.Lang.Object, the finalizer is executed.

Repro steps:

1. Extract and run the attached Xamarin.Android project.
2. Click the app button lots of times.

Expected results: It exits.

Actual results: It doesn't.
Comment 1 Jonathan Pryor 2015-09-30 12:30:41 UTC
An "equivalent" non-Xamarin.Android case, which works as expected:

  using System;
  using System.Threading;

  class MyClass {

    ~MyClass ()
      Console.WriteLine ("# jonp: MyClass.Finalize()");
      throw new Exception ("DIE DIE DIE");

    public void  Dispose ()

      GC.ReRegisterForFinalize (this);

  class App {
    public static void Main ()
      var t = new Thread(() => {
          var c = new MyClass ();
          c.Dispose ();
      t.Start ();
      t.Join ();



  $ mcs finalizer.cs
  $ mono finalizer.exe 
> # jonp: MyClass.Finalize()
> Unhandled Exception:
> System.Exception: DIE DIE DIE
>   at MyClass.Finalize () [0x00000] in <filename unknown>:0
Comment 2 Abhishek 2016-01-15 14:23:09 UTC
I have tried this issue and able to reproduce this issue at my end with the given sample. 

I have followed the same steps given in the bug description.

screencast: http://www.screencast.com/t/fkXXlQG3pvmn
Ide Log: https://gist.github.com/Abhishekk360/f00dc62fda02a2528204
Application Output: https://gist.github.com/Abhishekk360/2a7bcf03925ff827c615
adb Logcat: https://gist.github.com/Abhishekk360/732e720ab33c1c1a4842

As per comment 1 ,I have created the console application and followed the code snippet.

Output: https://gist.github.com/Abhishekk360/4ea348d5434629db9871

Environment Info:

=== Xamarin Studio ===

Version 5.10.2 (build 51)
Installation UUID: 93e693b0-b53d-40f4-b29c-b61ff5cbe892
	Mono 4.2.2 (explicit/b8ca425)
	GTK+ 2.24.23 (Raleigh theme)

	Package version: 402020028

=== Xamarin.Profiler ===

Location: /Applications/Xamarin Profiler.app/Contents/MacOS/Xamarin Profiler

=== Xamarin.Android ===

Version: (Enterprise Edition)
Android SDK: /Users/xamarin23/Desktop/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.3    (API level 18)
		4.4    (API level 19)
		4.4.87 (API level 20)
		5.0    (API level 21)
		5.1    (API level 22)
		6.0    (API level 23)

SDK Tools Version: 24.4.1
SDK Platform Tools Version: 23.1
SDK Build Tools Version: 23.0.2

Java SDK: /usr
java version "1.7.0_71"
Java(TM) SE Runtime Environment (build 1.7.0_71-b14)
Java HotSpot(TM) 64-Bit Server VM (build 24.71-b01, mixed mode)

=== Xamarin Android Player ===

Version: 0.6.1
Location: /Applications/Xamarin Android Player.app

=== Apple Developer Tools ===

Xcode 7.1 (9079)
Build 7B91b

=== Xamarin.Mac ===

Version: (Enterprise Edition)

=== Xamarin.iOS ===

Version: (Enterprise Edition)
Hash: c71a120
Branch: master
Build date: 2016-01-12 14:45:43-0500

=== Build Information ===

Release ID: 510020051
Git revision: 36453d5e06a1ad2cc8d261ad32e29abe0ed57c1f
Build date: 2016-01-12 17:33:10-05
Xamarin addins: ab25321e8918350655a7d46c86cfbaac3222fd3c
Build lane: monodevelop-lion-cycle6-c6sr1

=== Operating System ===

Mac OS X 10.10.5
Darwin 800BC.local 14.5.0 Darwin Kernel Version 14.5.0
    Wed Jul 29 02:26:53 PDT 2015
    root:xnu-2782.40.9~1/RELEASE_X86_64 x86_64