[Mono-list] Dispose method is never called

Giuseppe Greco gius.greco@bluewin.ch
Tue, 8 Jul 2003 18:01:03 +0200


>-- Original Message --
>Date: Tue, 08 Jul 2003 08:45:03 -0700
>From: "Richard Norman" <normri@samc.com>
>To: <gius.greco@bluewin.ch>,<mono-list@lists.ximian.com>
>Subject: Re: [Mono-list] Dispose method is never called
>
>
>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.

Mono doesn't execute the Finalizer at all. I've posted this problem
to bugzilla.ximian.com. Look at bug #45962 and get the source code
I've used to reproduce the problem (source code is attached as a tarball).

Try to execute that small program either with .NET on Windows or with
Mono on Linux, and you'll see the differences...

Gius_.

> 
>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 
>
>