[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> </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> </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> </DIV>
<DIV>You have brought an interesting problem. Any ideas anyone... My brain is
fried :-) (just kidding)</DIV>
<DIV> </DIV>
<DIV>Richard Norman</DIV>
<DIV>Web/Application Developer<BR><BR>>>> "Giuseppe Greco"
<gius.greco@bluewin.ch> 7/7/2003 10:30:55 PM >>><BR></DIV>
<DIV><BR>>-- Original Message --<BR>>From: "Giuseppe Greco"
<gius.greco@bluewin.ch><BR>>Subject: Re: [Mono-list] Dispose method is
never called<BR>>To: mono-list@lists.ximian.com<BR>>Cc:
normri@samc.com<BR>>Date: Tue, 8 Jul 2003 06:30:54
+0200<BR>><BR>><BR>><BR>>>-- Original Message --<BR>>>From:
"Giuseppe Greco" <gius.greco@bluewin.ch><BR>>>Subject: Re:
[Mono-list] Dispose method is never called<BR>>>To:
mono-list@lists.ximian.com<BR>>>Cc: normri@samc.com<BR>>>Date: Tue,
8 Jul 2003 06:25:33 +0200<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.<BR>>> <BR>>>> Is it called under windows? I think
it's not because MyThreadMethod is<BR>>>> accessing isDisposed field,
which belongs to the class instance.<BR>>>> That's<BR>>>> why
it's never disposed.<BR>>><BR>>>I've also tried an empty
thread function like this<BR>>><BR>>>private void
MyThreadMethod()<BR>>>{<BR>>> while (true)
{<BR>>> Thread.Sleep(100);<BR>>>
{<BR>>>}<BR>>><BR>>>... but the class is still never
disposed.<BR>>><BR>>>Gius_.<BR>><BR>>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>
if (!isDisposed) {<BR> isDisposed =
true;<BR><BR> if (disposing)
{<BR>
listener.Join();<BR> listener =
null;<BR><BR>
socket.Close();<BR> socket =
null;<BR> }<BR> }<BR>}<BR><BR>private void
Listen()<BR>{<BR> AsyncToken token = null;<BR> EndPoint endPoint =
null;<BR><BR> while (!isDisposed) {<BR> if
(socket.Poll(PollTimeOut, SelectMode.SelectRead))
{<BR> token = new AsyncToken(socket, new
byte[socket.Available]);<BR> endPoint = (EndPoint)
localEndPoint;<BR>
socket.BeginReceiveFrom(<BR> (byte[])
token.Data,<BR>
0,<BR> ((byte[])
token.Data).Length,<BR>
SocketFlags.None,<BR> ref
endPoint,<BR> new
AsyncCallback(AsyncReceive),<BR>
token);<BR> }<BR> }<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>>><BR>>>>
-Gonzalo<BR>>>
<BR>>><BR>>>_______________________________________________<BR>>>Mono-list
maillist - Mono-list@lists.ximian.com<BR>>><A
href="http://lists.ximian.com/mailman/listinfo/mono-list">http://lists.ximian.com/mailman/listinfo/mono-list</A><BR>><BR>>_______________________________________________<BR>>Mono-list
maillist - Mono-list@lists.ximian.com<BR>><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--