tags:

views:

53

answers:

3

A CustomPropertyList class is created in my form constructor.

form(String ^s)
{
    InitializeComponent();
    CustomPropertyList ^propertyList = gcnew CustomPropertyList(s);
...

The CustomPropertyList class has a destructor

CustomPropertyList::~CustomPropertyList()
{

    if (MessageBox::Show("Do you want to save your changes?","Editin",MessageBoxButtons::YesNo)==DialogResult::Yes)
...

Why is it not called when the program exits? (I know it's not as I don't see the message box and there's a breakpoint there)

I'm very grateful for any help

+4  A: 

Because this is not destructor, this is Dispose method (in C# terms). If client doesn't call Dispose, it is never called. On the other hand, finalizer (!CustomPropertyList) should be called, unless GC::SuppressFinalize is used to prevent it.

From C# code, ~CustomPropertyList can be called by using Dispose. From C++/CLI client code, it can be called by using delete operator.

See more details in this C++/CLI guru article: http://www.codeproject.com/KB/mcpp/cppclidtors.aspx

Alex Farber
Thanks - it worked very well - just added delete
Elliott
+1  A: 

From .net, a C++/CLI class looks like a class that implements the IDisposable interface. To invoke the destructor, you'll have to call Dispose on the object.

jdv
A: 

In C++/CLI, if you want objects to have lifetime controlled by the enclosing scope (for member subobjects, the same lifetime as the parent), declare them without the handle or pointer syntax.

e.g.

CustomPropertyList propertyList(s);

or for a member subobject:

ref class form
{
    CustomPropertyList propertyList;
    form(String^ s)
      : propertyList(s)
    {
       InitializeComponent();
    }
}
Ben Voigt