[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> </DIV>
<DIV>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)</DIV>
<DIV> </DIV>
<DIV>The IDispose interface is used to be able to clear up resources without
necessarily being garbage collected right away.</DIV>
<DIV> </DIV>
<DIV>Here are some articles on MSDN:</DIV>
<DIV><A
href="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">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</A></DIV>
<DIV> </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> </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> </DIV>
<DIV>Richard Norman</DIV>
<DIV>Web/Application Developer</DIV>
<DIV> </DIV>
<DIV>El dom, 06-07-2003 a las 17:20, Giuseppe Greco escribi=:<BR>> Hi
all,<BR>> <BR>> I've a class that starts a worker thread like<BR>>
this:<BR>> <BR>> class MyClass<BR>> {<BR>> bool
isDisposed = false;<BR>> Thread myThread;<BR>>
<BR>> public MyClass()<BR>>
{<BR>>
...<BR>> myThread = new Thread(new
ThreadStart(MyThreadMethod));<BR>>
myThread.Start();<BR>>
myThread.IsBackground = true;<BR>> }<BR>>
<BR>> ~MyClass()<BR>>
{<BR>> Dispose(false)<BR>> }<BR>>
<BR>> public void Dispose()<BR>>
{<BR>> Dispose(true);<BR>>
GC.SuppressFinalize(this);<BR>> }<BR>> <BR>>
protected virtual void Dispose(bool disposing)<BR>>
{<BR>> if (!isDisposed)
{<BR>> isDisposed = true;<BR>>
<BR>> myThread.Join(); // should wait
until
MyThreadMethod()<BR>>
// completes its work...<BR>> myThread =
null;<BR>>
<BR>> if (disposing)
{<BR>>
...<BR>>
}<BR>> }<BR>> }<BR>>
<BR>> private void MyThreadMethod()<BR>>
{<BR>> while (!isDisposed)
{<BR>>
...<BR>> {<BR>> }<BR>> }<BR>>
<BR>> Well, in the class above, the Dispose() method is never<BR>> called.
This is a problem if one needs to wait until the<BR>> thread has finished its
work -- Thread.Join() should block<BR>> until then.<BR>> <BR>> The
destructor -- ~MyClass() -- is never called.</DIV>
<DIV> </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> </DIV>
<DIV>-Gonzalo</DIV>
<DIV> </DIV>
<DIV><BR> </DIV></BODY></HTML>
--=_742A4F89.10710587--