Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does my destructor never run?

I have a blank Winform with a destructor method

public partial class Form1 : Form
{
    public Form1()
    {
        System.Diagnostics.Trace.WriteLine("Form1.Initialize " + this.GetHashCode().ToString());
        InitializeComponent();
    }
    ~Form1()
    {
        System.Diagnostics.Trace.WriteLine("Form1.Dispose " + this.GetHashCode().ToString());
    }
}

When the form is destroyed, I want it to write to the output window:

(Form1 opened)
Form1.Initialize 41149443
(Form1 closed)
Form1.Dispose 41149443

MSDN suggests 3 ways in implementing destructor:

  • ~Destructor() http://msdn.microsoft.com/en-us/library/66x5fx1b.aspx

  • IDisposable http://msdn.microsoft.com/en-us/library/system.idisposable.aspx

  • SafeHandle pattern http://msdn.microsoft.com/en-us/library/system.runtime.interopservices.safehandle.aspx

However, none of these ways write "Form1.Dispose 41149443" to the output Window. Therefore, I am not able to tell whether the form has been destroyed or not. Suggestions ?

Should I give up hope on achieving this due to uncertainty of garbage collector?

Is there another way to know whether Form1 has been garbage collected ?

like image 290
Jeson Martajaya Avatar asked Aug 03 '11 18:08

Jeson Martajaya


People also ask

Why is my destructor not being called?

There are two reasons that your destructors aren't being called, one is as kishor8dm pointed out that you are using the operator "new" and because of that the "delete" command must be called explicitly.

Why did the destructor not execute C#?

The destructor will only get called when the garbage collector decides to collect your instance. The garbage collector runs infrequently, typically only when it detects that there is memory pressure. The garbage collector collects ONLY orphaned collections.

How do you trigger a destructor?

A destructor is a member function that is invoked automatically when the object goes out of scope or is explicitly destroyed by a call to delete . A destructor has the same name as the class, preceded by a tilde ( ~ ). For example, the destructor for class String is declared: ~String() .

How do you trigger a destructor in C++?

Use the obj. ~ClassName() Notation to Explicitly Call a Destructor Function. Destructors are special functions that get executed when an object goes out of scope automatically or is deleted by an explicit call by the user.


1 Answers

Only one of the three ways to implement a destructor that you list actually involves a destructor, and that's ~Destructor().

If you implement IDisposable, and dispose of your object, then the code in Dispose will run, but there's no reason to think that your destructor will.

I think you chasing the impossible here. Destructors run as and when the garbage collector so decrees. It's not something that you have any control over. The GC is well within its rights to form the opinion that running destructors simply wastes time, and if there is plenty of memory it will form that opinion.

If you need predictable disposal, finalization etc., then use IDisposable.

like image 142
David Heffernan Avatar answered Sep 18 '22 06:09

David Heffernan