如何调用所有析构函数?

How to call all destructors?

我有一个大问题。当我抛出异常或调用 std::terminate 或 std::exit 时,对象析构函数不会运行。好吧,这就是问题所在,我的代码中充满了这些语句,因为我认为这些语句正在调用析构函数。

我该如何解决这个问题?我无法更改 类 的结构,我有 10K 的代码和许多 类.

void Function(int Stage)
{
    switch (Stage)
    {
        case 0: 
            //Somehow call the destructors
            break;
        case 1:
            //initialize
            break;
        default:
           //Give an error message and call the destructors
   }
}

析构函数应该 运行 在出现异常等情况时自动执行,这正是它们最初被发明的原因。

也许您使用 new 创建了对象?在这种情况下,您需要显式 delete 它们(然后析构函数将 运行)。