为什么我的析构函数永远不会被调用?我的、永远不会、函数

2023-09-06 23:34:29 作者:啭捔遇菿殇

一个CustomPropertyList类是在我的窗体构造函数创建的。

A CustomPropertyList class is created in my form constructor.

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

该CustomPropertyList类有一个析构函数

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)

我非常感谢所有帮助

推荐答案

由于这是不是析构函数,这是Dispose方法(在C#术语)。如果客户端没有调用Dispose,它永远不会被调用。另一方面,终结器(!CustomPropertyList)应该被调用,除非GC ::燮pressFinalize用于prevent它

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.

从C#code,〜CustomPropertyList可以通过使用所谓的处置。从C ++ / CLI客户端code,它可以通过使用delete操作符来调用。

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

本C ++ / CLI大师文章中看到更多细节:http://www.$c$cproject.com/KB/mcpp/cppclidtors.aspx

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

 
精彩推荐