在哪种情况下,将不会调用c ++析构函数? [英] In what kind of situation, c++ destructor will not be called?

查看:234
本文介绍了在哪种情况下,将不会调用c ++析构函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在c ++中,我们喜欢在析构函数中执行某些操作。但是在哪种情况下,不会调用析构函数?

In c++, we love to do something in the destructor. But in what kind of situation, destructor will not be called?

在以下情况下的示例:


  1. exit()在线程中调用

  2. 未处理的异常并退出

  3. TerminateProcess()(在Windows中)

  4. 热/冷重启计算机

  5. 突然无法使用计算机...

  1. exit() call in the thread
  2. unhandled exceptions and exit
  3. TerminateProcess() (in Windows)
  4. warm/cold reboot computer
  5. sudden out of power of computer...


推荐答案

这是每个C ++程序员都应该知道的一种情况:

This is one case every C++ programmer should know:

#include <stdio.h>

class EmbeddedObject {
   private:
      char *pBytes;
   public:
      EmbeddedObject() {
         pBytes = new char[1000];
      }
     ~EmbeddedObject() {
         printf("EmbeddedObject::~EmbeddedObject()\n");
         delete [] pBytes;
      }
};

class Base {
  public:
    ~Base(){
       printf("Base::~Base()\n");
  }
};

class Derived : public Base {
   private:
      EmbeddedObject emb;
   public:
      ~Derived() {
         printf("Derived::~Derived()\n");
      }
};


int main (int argc, const char * argv[])
{
  Derived *pd = new Derived();
  // later for some good reason, point to it using Base pointer
  Base* pb = pd;
  delete pb; 
}

〜Base()将被调用,但〜Derived()不会被调用。这意味着〜Derived()中的代码不会执行。它可能必须做一些重要的事情。此外,应该自动调用 EmbeddedObject 的析构函数,但不会自动调用它。因此, EmbeddedObject 没有机会释放其动态分配的数据。这会导致内存泄漏。

~Base() will be called but ~Derived() will not. This means the code in ~Derived() does not execute. It may have to do something important. Also it's EmbeddedObject's destructor should have been automatically called but is not. Therefore, EmbeddedObject does not get a chance to free its dynamically allocated data. This causes a memory leak.

解决方案,在类 Base virtual 中创建析构函数

Solution, make destructor in class Base virtual:

class Base {
  public:
    virtual ~Base() {
    }   
};

对上述程序进行此更改意味着将在此命令中调用所有析构函数: Derived ::〜Derived() EmbeddedObject ::〜EmbeddedObject() Base ::〜Base ()

Making this one change to the above program means all destructors will be called in this oder: Derived::~Derived(), EmbeddedObject::~EmbeddedObject(), Base::~Base()

一般请阅读析构函数。与您提到的其他情况相比,您可能更关心这些问题。例如,在断电的情况下,所有用于安全清理的赌注通常都是关闭的!

Read up on destructors in general. These kinds of problems are more likely to be something of concern to you than the other scenarios you mention. For example in the case of a power down, all bets for safe cleanup are usually off!

在C ++中,我们可以很好地控制按我们执行命令的顺序执行析构函数的调用希望他们发生,这是个好消息。但是,在编写的程序中,如果您不够谨慎,则有可能泄漏对象并且根本不会删除对象。

In C++ we have good control over enforcing the calling of destructors in the order we want them to happen, which is good news. However in the programs you write there is potential for your objects to be leaked and not deleted at all if you are not carefull enough.

这篇关于在哪种情况下,将不会调用c ++析构函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆