[Mono-list] Dispose method is never called

Richard Norman normri@samc.com
Mon, 07 Jul 2003 09:45:05 -0700


This is a MIME message. If you are reading this text, you may want to 
consider changing to a mail reader or gateway that understands how to 
properly handle MIME multipart messages.

--=_742A4F89.10710587
Content-Type: text/plain; charset=US-ASCII
Content-Transfer-Encoding: 7bit

Please someone correct me if I am wrong,
 
What I believe you created is a finalizer.  Dispose is implemented as a
method you call on the object to clean up resources. The finalizer is
not called until the object is actually garbage collected. So it could
sit in memory for an undetermined amount of time before the finalizer is
called. (after writing this and reading two more times, Gonzalo does
have a point)
 
The IDispose interface is used to be able to clear up resources without
necessarily being garbage collected right away.
 
Here are some articles on MSDN:
http://search.microsoft.com/default.asp?qu=IDispose&boolean=ALL&so=RECCNT&p=1&nq=NEW&ig=03&ig=04&i=99&ig=05&i=99&ig=02&i=42&i=43&i=44&i=45&i=46&i=47&i=48&i=49&i=50&i=51&i=52&i=53&i=54&i=55&i=56&i=57&i=58&i=59&i=60&i=61&i=62&i=63&ig=07&i=99&ig=01&i=00&i=01&i=02&i=03&i=04&i=05&i=06&i=07&i=08&i=09&i=10&i=11&i=12&i=13&i=14&i=15&i=16&i=17&i=18&i=19&i=20&i=21&i=22&i=23&i=24&i=25&i=26&i=27&i=28&i=29&i=30&i=31&i=32&i=33&i=34&i=35&i=36&i=37&i=38&i=39&i=40&i=41&i=99&siteid=us/dev
 
This article is particularly good...
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dncscol/html/deepc10192000.asp
 
Now I may be off base at what's being asked, but In general if you
follow the linked articles, it generally should work.
 
Richard Norman
Web/Application Developer
 
El dom, 06-07-2003 a las 17:20, Giuseppe Greco escribi=:
> 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.
 
Is it called under windows? I think it's not because MyThreadMethod is
accessing isDisposed field, which belongs to the class instance.
That's
why it's never disposed.
 
-Gonzalo
 

 

--=_742A4F89.10710587
Content-Type: text/html; charset=ISO-8859-1
Content-Transfer-Encoding: 8bit
Content-Description: HTML

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD>
<META http-equiv=Content-Type content="text/html; charset=windows-1252">
<META content="MSHTML 6.00.2800.1173" name=GENERATOR></HEAD>
<BODY style="MARGIN: 4px 4px 1px; FONT: 12pt Arial">
<DIV>Please someone correct me if I am wrong,</DIV>
<DIV>&nbsp;</DIV>
<DIV>What I believe you created is a finalizer.&nbsp; Dispose is implemented as 
a method you call on the object to clean up resources. The finalizer is not 
called until the object is actually garbage collected. So it could sit in memory 
for an undetermined amount of time before the finalizer is called. (after 
writing this and reading two more times, Gonzalo does have a point)</DIV>
<DIV>&nbsp;</DIV>
<DIV>The IDispose interface is used to be able to clear up resources without 
necessarily being garbage collected right away.</DIV>
<DIV>&nbsp;</DIV>
<DIV>Here are some articles on MSDN:</DIV>
<DIV><A 
href="http://search.microsoft.com/default.asp?qu=IDispose&amp;boolean=ALL&amp;so=RECCNT&amp;p=1&amp;nq=NEW&amp;ig=03&amp;ig=04&amp;i=99&amp;ig=05&amp;i=99&amp;ig=02&amp;i=42&amp;i=43&amp;i=44&amp;i=45&amp;i=46&amp;i=47&amp;i=48&amp;i=49&amp;i=50&amp;i=51&amp;i=52&amp;i=53&amp;i=54&amp;i=55&amp;i=56&amp;i=57&amp;i=58&amp;i=59&amp;i=60&amp;i=61&amp;i=62&amp;i=63&amp;ig=07&amp;i=99&amp;ig=01&amp;i=00&amp;i=01&amp;i=02&amp;i=03&amp;i=04&amp;i=05&amp;i=06&amp;i=07&amp;i=08&amp;i=09&amp;i=10&amp;i=11&amp;i=12&amp;i=13&amp;i=14&amp;i=15&amp;i=16&amp;i=17&amp;i=18&amp;i=19&amp;i=20&amp;i=21&amp;i=22&amp;i=23&amp;i=24&amp;i=25&amp;i=26&amp;i=27&amp;i=28&amp;i=29&amp;i=30&amp;i=31&amp;i=32&amp;i=33&amp;i=34&amp;i=35&amp;i=36&amp;i=37&amp;i=38&amp;i=39&amp;i=40&amp;i=41&amp;i=99&amp;siteid=us/dev">http://search.microsoft.com/default.asp?qu=IDispose&amp;boolean=ALL&amp;so=RECCNT&amp;p=1&amp;nq=NEW&amp;ig=03&amp;ig=04&amp;i=99&amp;ig=05&amp;i=99&amp;ig=02&amp;i=42&amp;i=43&amp;i=44&amp;i=45&amp;i=46&amp;i=47&amp;i=48&amp;i=49&amp;i=50&amp;i=51&amp;i=52&amp;i=53&amp;i=54&amp;i=55&amp;i=56&amp;i=57&amp;i=58&amp;i=59&amp;i=60&amp;i=61&amp;i=62&amp;i=63&amp;ig=07&amp;i=99&amp;ig=01&amp;i=00&amp;i=01&amp;i=02&amp;i=03&amp;i=04&amp;i=05&amp;i=06&amp;i=07&amp;i=08&amp;i=09&amp;i=10&amp;i=11&amp;i=12&amp;i=13&amp;i=14&amp;i=15&amp;i=16&amp;i=17&amp;i=18&amp;i=19&amp;i=20&amp;i=21&amp;i=22&amp;i=23&amp;i=24&amp;i=25&amp;i=26&amp;i=27&amp;i=28&amp;i=29&amp;i=30&amp;i=31&amp;i=32&amp;i=33&amp;i=34&amp;i=35&amp;i=36&amp;i=37&amp;i=38&amp;i=39&amp;i=40&amp;i=41&amp;i=99&amp;siteid=us/dev</A></DIV>
<DIV>&nbsp;</DIV>
<DIV>This article is particularly good...</DIV>
<DIV><A 
href="http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dncscol/html/deepc10192000.asp">http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dncscol/html/deepc10192000.asp</A></DIV>
<DIV>&nbsp;</DIV>
<DIV>Now I may be off base at what's being asked, but In general if you follow 
the linked articles, it generally should work.</DIV>
<DIV>&nbsp;</DIV>
<DIV>Richard Norman</DIV>
<DIV>Web/Application Developer</DIV>
<DIV>&nbsp;</DIV>
<DIV>El dom, 06-07-2003 a las 17:20, Giuseppe Greco escribi=:<BR>&gt; Hi 
all,<BR>&gt; <BR>&gt; I've a class that starts a worker thread like<BR>&gt; 
this:<BR>&gt; <BR>&gt; class MyClass<BR>&gt; {<BR>&gt;&nbsp;&nbsp; bool 
isDisposed = false;<BR>&gt;&nbsp;&nbsp; Thread myThread;<BR>&gt; 
<BR>&gt;&nbsp;&nbsp; public MyClass()<BR>&gt;&nbsp;&nbsp; 
{<BR>&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
...<BR>&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; myThread = new Thread(new 
ThreadStart(MyThreadMethod));<BR>&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
myThread.Start();<BR>&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
myThread.IsBackground = true;<BR>&gt;&nbsp;&nbsp;&nbsp; }<BR>&gt; 
<BR>&gt;&nbsp;&nbsp; ~MyClass()<BR>&gt;&nbsp;&nbsp; 
{<BR>&gt;&nbsp;&nbsp;&nbsp;&nbsp; Dispose(false)<BR>&gt;&nbsp;&nbsp; }<BR>&gt; 
<BR>&gt;&nbsp;&nbsp; public void Dispose()<BR>&gt;&nbsp;&nbsp; 
{<BR>&gt;&nbsp;&nbsp;&nbsp;&nbsp; Dispose(true);<BR>&gt;&nbsp;&nbsp;&nbsp;&nbsp; 
GC.SuppressFinalize(this);<BR>&gt;&nbsp;&nbsp; }<BR>&gt; <BR>&gt;&nbsp;&nbsp; 
protected virtual void Dispose(bool disposing)<BR>&gt;&nbsp;&nbsp; 
{<BR>&gt;&nbsp;&nbsp;&nbsp;&nbsp; if (!isDisposed) 
{<BR>&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; isDisposed = true;<BR>&gt; 
<BR>&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; myThread.Join(); // should wait 
until 
MyThreadMethod()<BR>&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
// completes its work...<BR>&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; myThread = 
null;<BR>&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
<BR>&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (disposing) 
{<BR>&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
...<BR>&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
}<BR>&gt;&nbsp;&nbsp;&nbsp;&nbsp; }<BR>&gt;&nbsp;&nbsp; }<BR>&gt; 
<BR>&gt;&nbsp;&nbsp; private void MyThreadMethod()<BR>&gt;&nbsp;&nbsp; 
{<BR>&gt;&nbsp;&nbsp;&nbsp;&nbsp; while (!isDisposed) 
{<BR>&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
...<BR>&gt;&nbsp;&nbsp;&nbsp;&nbsp; {<BR>&gt;&nbsp;&nbsp; }<BR>&gt; }<BR>&gt; 
<BR>&gt; Well, in the class above, the Dispose() method is never<BR>&gt; called. 
This is a problem if one needs to wait until the<BR>&gt; thread has finished its 
work -- Thread.Join() should block<BR>&gt; until then.<BR>&gt; <BR>&gt; The 
destructor -- ~MyClass() -- is never called.</DIV>
<DIV>&nbsp;</DIV>
<DIV>Is it called under windows? I think it's not because MyThreadMethod 
is<BR>accessing isDisposed field, which belongs to the class instance. 
That's<BR>why it's never disposed.</DIV>
<DIV>&nbsp;</DIV>
<DIV>-Gonzalo</DIV>
<DIV>&nbsp;</DIV>
<DIV><BR>&nbsp;</DIV></BODY></HTML>

--=_742A4F89.10710587--