[Mono-bugs] [Bug 45962][Cri] New - Destructor (or Finalize() if you prefer) is never invoked

bugzilla-daemon@rocky.ximian.com bugzilla-daemon@rocky.ximian.com
Tue, 8 Jul 2003 05:27:13 -0400 (EDT)


Please do not reply to this email- if you want to comment on the bug, go to the
URL shown below and enter your comments there.

Changed by giuseppe.greco@agamura.com.

http://bugzilla.ximian.com/show_bug.cgi?id=45962

--- shadow/45962	Tue Jul  8 05:27:13 2003
+++ shadow/45962.tmp.10493	Tue Jul  8 05:27:13 2003
@@ -0,0 +1,96 @@
+Bug#: 45962
+Product: Mono/Runtime
+Version: unspecified
+OS: Red Hat 9.0
+OS Details: 
+Status: NEW   
+Resolution: 
+Severity: 
+Priority: Critical
+Component: misc
+AssignedTo: mono-bugs@ximian.com                            
+ReportedBy: giuseppe.greco@agamura.com               
+QAContact: mono-bugs@ximian.com
+TargetMilestone: ---
+URL: 
+Summary: Destructor (or Finalize() if you prefer) is never invoked
+
+The class destructor (or Finalize() method) is never
+called by the GC.
+
+
+Steps to reproduce the problem:
+1. Write a small program like this:
+
+using System;
+
+public class MyFinalizer : IDisposable
+{
+  bool isDisposed = false;
+
+  public MyFinalizer()
+  {
+    Console.WriteLine("Constructor");
+  }
+
+  ~MyFinalizer()
+  {
+    Console.WriteLine("Destructor");
+    Dispose(false);
+  }
+
+  public void Dispose()
+  {
+    Dispose(true);
+    GC.SuppressFinalize(this);
+  }
+
+  protected virtual void Dispose(bool disposing)
+  {
+    if (!isDisposed) {
+      Console.WriteLine("Free unmanaged resources.");
+
+      if (disposing) {
+        Console.WriteLine("Finalize will be suppressed.");
+        Console.WriteLine("Free resource manually.");
+      }
+
+      isDisposed = true;
+    }
+  }
+
+  public static void Main()
+  {
+    MyFinalizer f = new MyFinalizer();
+    Console.WriteLine("Bye!");
+  }
+}
+
+
+2. Compile it:
+
+   mcs -t:exe -out:finalizer.exe *.cs
+
+
+3. Run it:
+
+   mono ./finalizer.exe
+
+
+Actual Results:
+~MyFinalizer() is never called, and of course,
+the message "Destructor" is never printed.
+
+
+Expected Results:
+When the application exits, the GC should call
+Finalize(), and the code inside ~MyFinalizer
+should be executed.
+
+
+How often does this happen? 
+Always.
+
+
+Additional Information:
+The code above works fine with .NET on Windows.