[Mono-list] Dispose method is never called

Joeri Belis joeri.belis@nollekens.be
Wed, 23 Jul 2003 08:41:20 +0200


Dit is een meerdelig bericht in MIME-indeling.

------=_NextPart_000_0080_01C350F6.308D9D70
Content-Type: text/plain;
	charset="iso-8859-1"
Content-Transfer-Encoding: 7bit

Both programs give me the same output on mono 0.24

C:\DOCUME~1\joeri\MIJNDO~1\SHARPD~1\HELLOW~1>fin2
Constructor
Bye!
Destructor
Free unmanaged resources.

C:\DOCUME~1\joeri\MIJNDO~1\SHARPD~1\HELLOW~1>fin
Constructor
Bye!
Destructor
Free unmanaged resources.


----- Original Message ----- 
From: "Giuseppe Greco" <gius.greco@bluewin.ch>
To: "Joeri Belis" <joeri.belis@nollekens.be>; <mono-list@lists.ximian.com>
Sent: Tuesday, July 22, 2003 7:44 PM
Subject: Re: [Mono-list] Dispose method is never called


> 
> >-- Original Message --
> >From: "Joeri Belis" <joeri.belis@nollekens.be>
> >To: <mono-list@lists.ximian.com>
> >Subject: Re: [Mono-list] Dispose method is never called
> >Date: Tue, 22 Jul 2003 11:54:08 +0200
> >
> >
> >It compiled and ran the "MyFinalizer()" example with and without lines (
> >13 -> 15 )
> >but is get the same output results. Is this what to expect?
> >
> >C:\DOCUME~1\joeri\MIJNDO~1\SHARPD~1\HELLOW~1>fin.exe
> >Constructor
> >Bye!
> >Destructor
> >Free unmanaged resources.
> 
> Yes, but when I run the example, I don't get the last two
> messages...
> 
> Have you commented out lines 13, 14, and 15?
> 
> Gius_.
> >
> >>
> >> Gonzalo, in mono-threaded applications the destructor
> >> (or Finalize method) is called, but in multi-threaded
> >> ones it isn't. Please try the attached program. As you
> >> will see, the thread method is brutally interrupted and
> >> the finalizer is never called.
> >>
> >> If you comment out line 13, 14, and 15 in finalizer.cs,
> >> then it works fine.
> >>
> >> I hope that helps...
> >>
> >> Gius_.
> >>
> >
> >
> >_______________________________________________
> >Mono-list maillist  -  Mono-list@lists.ximian.com
> >http://lists.ximian.com/mailman/listinfo/mono-list
> 
> 
> 
> 
------=_NextPart_000_0080_01C350F6.308D9D70
Content-Type: application/octet-stream;
	name="fin.cs"
Content-Transfer-Encoding: 7bit
Content-Disposition: attachment;
	filename="fin.cs"

using System;
using System.Threading;

public class MyFinalizer : IDisposable
{
  Thread thread;
  bool isDisposed = false;

  public MyFinalizer()
  {
    Console.WriteLine("Constructor");

    thread = new Thread(new ThreadStart(DoSomething));
    thread.Start();
    thread.IsBackground = true;
  }

  private void DoSomething()
  {
    while (!isDisposed) {
      Thread.Sleep(100);
    }
  }

  ~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!");
  }
}
------=_NextPart_000_0080_01C350F6.308D9D70
Content-Type: application/octet-stream;
	name="fin2.cs"
Content-Transfer-Encoding: 7bit
Content-Disposition: attachment;
	filename="fin2.cs"

using System;
using System.Threading;

public class MyFinalizer : IDisposable
{
  Thread thread;
  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!");
  }
}
------=_NextPart_000_0080_01C350F6.308D9D70--