[Mono-list] Destructor calling not working properly under Linux?

Jonathan Gilbert 2a5gjx302@sneakemail.com
Tue, 11 Jan 2005 14:35:04 -0500


At 07:07 PM 11/01/2005 +0100, Jurek Bartuszek wrote:
>Hi! I have a problem regarding destroying objects at the end of the
>running program. The problem is that not all objects are properly
>destroyed (or at least, destructors called):
[snip]
>I post my message here, because Windows displayed all three messages
>correctly. Do you know how to fix this?

The problem here is that the specification for .NET/C# does not guarantee
the behaviour you want. Microsoft's runtime is nice enough to try to
provide it, but to guarantee interoperability, you should use the
IDisposable interface, and manually Dispose() anything that *must* be
released when the program exits.

The reason the specification doesn't guarantee the behaviour is that it is
a very hard behaviour to guarantee generally, and it is equally difficult
to try to specify what situations it should be guaranteed in. Consider the
following situation:

class HahaCantGetMe
{
  ~HahaCantGetMe()
  {
    System.GC.ReRegisterForFinalize(this);
  }

  static void Main()
  {
    new HahaCantGetMe();
  }
}

If the behaviour you want is guaranteed without restrictions, then this
program can never end! Nevertheless, it does end under Windows, albeit with
a bit of a delay. Windows enforces a timeout of 5 seconds or so while
shutting down the application; any objects not yet finalized by that point
meet the same fate you saw in mono.

This is my understanding of the situation, anyway...

Jonathan Gilbert