[Mono-list] Dispose method is never called

Giuseppe Greco giuseppe.greco@agamura.com
06 Jul 2003 18:55:36 +0200


On Sun, 2003-07-06 at 18:20, James F. Bellinger wrote:
> It's possible the thread is being garbage-collected before the class itself.
> There's no guarantee that any object will be swept before or after any
> other, and if MyClass is dead, myThread is certainly also. Are you sure the
> destructor is not being called?

Yes, I inserted a trace at the beginning of the destructor, and at the
beginning of the Dispose() method, but none of them have been
executed...

> It's possible that it is getting up to
> myThread.Join() and having an exception because myThread has already been
> collected. If there is an exception in a destructor the destructor will be
> silently exited.

That's not the case... In my original code, the thread method contains
a loop that continuously call Socket.Pool(); The Poll() method throws
then an exception with the message "Interrupted"! That's probably
because the thread was just brutally interrupted.

My thread method has to exit gracefully, and that's why I call the
Thread.Join() method in Dispose()...

I've tried the same code with .NET on Windows, and all works fine
.. I don't know it that helps.

Gius_.

> 
> ----- Original Message ----- 
> From: "Giuseppe Greco" <giuseppe.greco@agamura.com>
> To: "Mono" <mono-list@ximian.com>
> Sent: Sunday, July 06, 2003 11:20 AM
> Subject: [Mono-list] Dispose method is never called
> 
> 
> > Hi all,
> >
> > I've a class that starts a worker thread like
> > this:
> >
> > class MyClass
> > {
> >   bool isDisposed = false;
> >   Thread myThread;
> >
> >   public MyClass()
> >   {
> >       ...
> >       myThread = new Thread(new ThreadStart(MyThreadMethod));
> >       myThread.Start();
> >       myThread.IsBackground = true;
> >    }
> >
> >   ~MyClass()
> >   {
> >     Dispose(false)
> >   }
> >
> >   public void Dispose()
> >   {
> >     Dispose(true);
> >     GC.SuppressFinalize(this);
> >   }
> >
> >   protected virtual void Dispose(bool disposing)
> >   {
> >     if (!isDisposed) {
> >       isDisposed = true;
> >
> >       myThread.Join(); // should wait until MyThreadMethod()
> >                        // completes its work...
> >       myThread = null;
> >
> >       if (disposing) {
> >         ...
> >       }
> >     }
> >   }
> >
> >   private void MyThreadMethod()
> >   {
> >     while (!isDisposed) {
> >       ...
> >     {
> >   }
> > }
> >
> > Well, in the class above, the Dispose() method is never
> > called. This is a problem if one needs to wait until the
> > thread has finished its work -- Thread.Join() should block
> > until then.
> >
> > The destructor -- ~MyClass() -- is never called.
> >
> > Gius_.
> 
> _______________________________________________
> Mono-list maillist  -  Mono-list@lists.ximian.com
> http://lists.ximian.com/mailman/listinfo/mono-list
-- 
----------------------------------------
Giuseppe Greco

::agamura::

phone:  +41 (0)91 604 67 65
mobile: +41 (0)76 390 60 32
email:  giuseppe.greco@agamura.com
web:    www.agamura.com
----------------------------------------