[Mono-list] Dispose method is never called

Richard Norman normri@samc.com
Tue, 08 Jul 2003 08:45:03 -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.

--=_BAE48200.6A0B7E13
Content-Type: text/plain; charset=US-ASCII
Content-Transfer-Encoding: 7bit

Sorry I missed the part where you said this works fine in Windows.
 
If this is working in Windows under the .NET Framework, then I would
look to see if the pattern for garbage collection is being followed by
Mono.
 
It could be one of two issues here. Either the .NET Framework is not
the "standard" spec, or the Mono garbage collection is not executing at
the appropriate times and executing the Finalizer properly.
 
You have brought an interesting problem. Any ideas anyone... My brain
is fried :-) (just kidding)
 
Richard Norman
Web/Application Developer

>>> "Giuseppe Greco" <gius.greco@bluewin.ch> 7/7/2003 10:30:55 PM >>>


>-- Original Message --
>From: "Giuseppe Greco" <gius.greco@bluewin.ch>
>Subject: Re: [Mono-list] Dispose method is never called
>To: mono-list@lists.ximian.com 
>Cc: normri@samc.com 
>Date: Tue, 8 Jul 2003 06:30:54 +0200
>
>
>
>>-- Original Message --
>>From: "Giuseppe Greco" <gius.greco@bluewin.ch>
>>Subject: Re: [Mono-list] Dispose method is never called
>>To: mono-list@lists.ximian.com 
>>Cc: normri@samc.com 
>>Date: Tue, 8 Jul 2003 06:25:33 +0200
>>
>>
>>>> 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.
>>
>>I've also  tried an empty thread function like this
>>
>>private void MyThreadMethod()
>>{
>>  while (true) {
>>    Thread.Sleep(100);
>>  {
>>}
>>
>>... but the class is still never disposed.
>>
>>Gius_.
>
>Some additional information

Sorry, I've accidentally pressed the "send" button...

By the way, here below is my original code:


protected virtual void Dispose(bool disposing)
{
  if (!isDisposed) {
    isDisposed = true;

    if (disposing) {
      listener.Join();
      listener = null;

      socket.Close();
      socket = null;
    }
  }
}

private void Listen()
{
  AsyncToken token = null;
  EndPoint endPoint = null;

  while (!isDisposed) {
    if (socket.Poll(PollTimeOut, SelectMode.SelectRead)) {
      token = new AsyncToken(socket, new byte[socket.Available]);
      endPoint = (EndPoint) localEndPoint;
      socket.BeginReceiveFrom(
        (byte[]) token.Data,
        0,
        ((byte[]) token.Data).Length,
        SocketFlags.None,
        ref endPoint,
        new AsyncCallback(AsyncReceive),
        token);
    }
  }
}

As you can see, Dispose set isDisposed to false, letting
the Listen thread function stop and exit. So, the fact that
the thread function uses an instance field should not be a
problem at all. Furthermore, this works with .NET.

As I've written in my previous email, even if the thread
method doesn't refer instance fields, the destructor
(or Finalize method if you prefer) is never called.

The Listen method above continuously call the Poll()
method to see if new data is available... but since the
class is just dropped without finalizing, it can occur
that Poll() is interrupted brutally (and that's why I
often get a SocketException containing the message
"Interrupted").

Gius_.

>>
>>> -Gonzalo
>> 
>>
>>_______________________________________________
>>Mono-list maillist  -  Mono-list@lists.ximian.com 
>>http://lists.ximian.com/mailman/listinfo/mono-list 
>
>_______________________________________________
>Mono-list maillist  -  Mono-list@lists.ximian.com 
>http://lists.ximian.com/mailman/listinfo/mono-list 



--=_BAE48200.6A0B7E13
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>Sorry I missed the part where you said this works fine in Windows.</DIV>
<DIV>&nbsp;</DIV>
<DIV>If this is working in Windows under the .NET Framework, then I would look 
to see if the pattern for garbage collection is being followed by Mono.</DIV>
<DIV>&nbsp;</DIV>
<DIV>It could be one of two issues here. Either the .NET Framework is not the 
"standard" spec, or the Mono garbage collection is not executing at the 
appropriate times and executing the Finalizer properly.</DIV>
<DIV>&nbsp;</DIV>
<DIV>You have brought an interesting problem. Any ideas anyone... My brain is 
fried :-) (just kidding)</DIV>
<DIV>&nbsp;</DIV>
<DIV>Richard Norman</DIV>
<DIV>Web/Application Developer<BR><BR>&gt;&gt;&gt; "Giuseppe Greco" 
&lt;gius.greco@bluewin.ch&gt; 7/7/2003 10:30:55 PM &gt;&gt;&gt;<BR></DIV>
<DIV><BR>&gt;-- Original Message --<BR>&gt;From: "Giuseppe Greco" 
&lt;gius.greco@bluewin.ch&gt;<BR>&gt;Subject: Re: [Mono-list] Dispose method is 
never called<BR>&gt;To: mono-list@lists.ximian.com<BR>&gt;Cc: 
normri@samc.com<BR>&gt;Date: Tue, 8 Jul 2003 06:30:54 
+0200<BR>&gt;<BR>&gt;<BR>&gt;<BR>&gt;&gt;-- Original Message --<BR>&gt;&gt;From: 
"Giuseppe Greco" &lt;gius.greco@bluewin.ch&gt;<BR>&gt;&gt;Subject: Re: 
[Mono-list] Dispose method is never called<BR>&gt;&gt;To: 
mono-list@lists.ximian.com<BR>&gt;&gt;Cc: normri@samc.com<BR>&gt;&gt;Date: Tue, 
8 Jul 2003 06:25:33 +0200<BR>&gt;&gt;<BR>&gt;&gt;<BR>&gt;&gt;&gt;&gt; Well, in 
the class above, the Dispose() method is never<BR>&gt;&gt;&gt;&gt; called. This 
is a problem if one needs to wait until the<BR>&gt;&gt;&gt;&gt; thread has 
finished its work -- Thread.Join() should block<BR>&gt;&gt;&gt;&gt; until 
then.<BR>&gt;&gt;&gt;&gt;<BR>&gt;&gt;&gt;&gt; The destructor -- ~MyClass() -- is 
never called.<BR>&gt;&gt; <BR>&gt;&gt;&gt; Is it called under windows? I think 
it's not because MyThreadMethod is<BR>&gt;&gt;&gt; accessing isDisposed field, 
which belongs to the class instance.<BR>&gt;&gt;&gt; That's<BR>&gt;&gt;&gt; why 
it's never disposed.<BR>&gt;&gt;<BR>&gt;&gt;I've also&nbsp; tried an empty 
thread function like this<BR>&gt;&gt;<BR>&gt;&gt;private void 
MyThreadMethod()<BR>&gt;&gt;{<BR>&gt;&gt;&nbsp; while (true) 
{<BR>&gt;&gt;&nbsp;&nbsp;&nbsp; Thread.Sleep(100);<BR>&gt;&gt;&nbsp; 
{<BR>&gt;&gt;}<BR>&gt;&gt;<BR>&gt;&gt;... but the class is still never 
disposed.<BR>&gt;&gt;<BR>&gt;&gt;Gius_.<BR>&gt;<BR>&gt;Some additional 
information<BR><BR>Sorry, I've accidentally pressed the "send" 
button...<BR><BR>By the way, here below is my original 
code:<BR><BR><BR>protected virtual void Dispose(bool disposing)<BR>{<BR>&nbsp; 
if (!isDisposed) {<BR>&nbsp;&nbsp;&nbsp; isDisposed = 
true;<BR><BR>&nbsp;&nbsp;&nbsp; if (disposing) 
{<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
listener.Join();<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; listener = 
null;<BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
socket.Close();<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; socket = 
null;<BR>&nbsp;&nbsp;&nbsp; }<BR>&nbsp; }<BR>}<BR><BR>private void 
Listen()<BR>{<BR>&nbsp; AsyncToken token = null;<BR>&nbsp; EndPoint endPoint = 
null;<BR><BR>&nbsp; while (!isDisposed) {<BR>&nbsp;&nbsp;&nbsp; if 
(socket.Poll(PollTimeOut, SelectMode.SelectRead)) 
{<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; token = new AsyncToken(socket, new 
byte[socket.Available]);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; endPoint = (EndPoint) 
localEndPoint;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
socket.BeginReceiveFrom(<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (byte[]) 
token.Data,<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
0,<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ((byte[]) 
token.Data).Length,<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
SocketFlags.None,<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ref 
endPoint,<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; new 
AsyncCallback(AsyncReceive),<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
token);<BR>&nbsp;&nbsp;&nbsp; }<BR>&nbsp; }<BR>}<BR><BR>As you can see, Dispose 
set isDisposed to false, letting<BR>the Listen thread function stop and exit. 
So, the fact that<BR>the thread function uses an instance field should not be 
a<BR>problem at all. Furthermore, this works with .NET.<BR><BR>As I've written 
in my previous email, even if the thread<BR>method doesn't refer instance 
fields, the destructor<BR>(or Finalize method if you prefer) is never 
called.<BR><BR>The Listen method above continuously call the Poll()<BR>method to 
see if new data is available... but since the<BR>class is just dropped without 
finalizing, it can occur<BR>that Poll() is interrupted brutally (and that's why 
I<BR>often get a SocketException containing the 
message<BR>"Interrupted").<BR><BR>Gius_.<BR><BR>&gt;&gt;<BR>&gt;&gt;&gt; 
-Gonzalo<BR>&gt;&gt; 
<BR>&gt;&gt;<BR>&gt;&gt;_______________________________________________<BR>&gt;&gt;Mono-list 
maillist&nbsp; -&nbsp; Mono-list@lists.ximian.com<BR>&gt;&gt;<A 
href="http://lists.ximian.com/mailman/listinfo/mono-list">http://lists.ximian.com/mailman/listinfo/mono-list</A><BR>&gt;<BR>&gt;_______________________________________________<BR>&gt;Mono-list 
maillist&nbsp; -&nbsp; Mono-list@lists.ximian.com<BR>&gt;<A 
href="http://lists.ximian.com/mailman/listinfo/mono-list">http://lists.ximian.com/mailman/listinfo/mono-list</A><BR><BR></DIV></BODY></HTML>

--=_BAE48200.6A0B7E13--