首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >为什么在这个程序中不调用析构函数?

为什么在这个程序中不调用析构函数?
EN

Stack Overflow用户
提问于 2014-03-03 20:20:21
回答 3查看 104关注 0票数 0

我做了一个实例化这个类的函数,并做了一些操作。在函数返回之后,这意味着作用域已经结束。一旦作用域结束,它就应该调用类的析构函数。但是,我没有看到析构函数被调用。背后的原因是什么?我是不是漏掉了什么?

代码语言:javascript
复制
/***

The static member in the class is just a declaration, whose namespace scope is limited to the class.
One need to define it in the corresponding source of the class header. This way, the static member is
allocated a space on the Data segment. In case the static member is not defined, then the linker will
throw an error.

**/

class category_to_subcategory
{
     /** static members have just declaration not definition **/
     static QMultiMap<QString,QString>m_cat_to_subcat;

 public:
     /** The const can give a definition to it within the class, thus no need to define it **/
     /** C++ allows only "integer const static" types to be defined within a class */
     /** both are same const static or static const **/
     const static  int categ_const = 10;
     static void insert_subcat();
     /** template **/
     QList<QString> get_subcat(QString &cat);
     QList<QString> get_subcat(const QString &cat);
     ~category_to_subcategory();
};

/** When you want to map a string to string, you can use QMultimap **/
/** It is quite easy to get the values using QMultimap **/
/** definition of m_cat_to_subcat, the namescope is bind to the class name - category_to_subcategory  **/
QMultiMap<QString,QString> category_to_subcategory::m_cat_to_subcat;

 void category_to_subcategory::insert_subcat()
 {
     qDebug()<<__PRETTY_FUNCTION__;
     m_cat_to_subcat.clear();
     m_cat_to_subcat.insert("OS", "Linux");
     m_cat_to_subcat.insert("OS", "Windows");
     m_cat_to_subcat.insert("OS", "MAC");
     m_cat_to_subcat.insert("SHELL", "BASH");
     m_cat_to_subcat.insert("SHELL", "KSH");
     m_cat_to_subcat.insert("SHELL", "CSH");
     m_cat_to_subcat.insert("MOUSE", "WIRED");
     m_cat_to_subcat.insert("MOUSE", "WIRELESS");

 }

 QList<QString> category_to_subcategory::get_subcat(QString &cat)
 {
     qDebug()<<__PRETTY_FUNCTION__;
     if(category_to_subcategory::m_cat_to_subcat.empty()) {
          category_to_subcategory::insert_subcat();
      }
      QList<QString> subcat_list = m_cat_to_subcat.values(cat);
      return subcat_list;
 }

 QList<QString> category_to_subcategory::get_subcat(const QString &cat)
 {
     qDebug()<<__PRETTY_FUNCTION__;
     if(category_to_subcategory::m_cat_to_subcat.empty()) {
          category_to_subcategory::insert_subcat();
     }
     QList<QString> subcat_list = m_cat_to_subcat.values(cat);
     return subcat_list;
 }

category_to_subcategory::~category_to_subcategory()
{
   qDebug()<<__PRETTY_FUNCTION__;
   delete this;
}

void function_to_QMAP()
{
    qDebug()<<__PRETTY_FUNCTION__;
    category_to_subcategory *cat_to_subcat_Instance = new category_to_subcategory;
    QString cat = "OS";
    qDebug()<<cat_to_subcat_Instance->get_subcat(cat);
    cat = "SHELL";
    qDebug()<<cat_to_subcat_Instance->get_subcat(cat);
    cat = "MOUSE";
    qDebug()<<cat_to_subcat_Instance->get_subcat(cat);
    /** Passing just the string will throw an error **/
    //qDebug()<<cat_to_subcat_Instance->get_subcat("MOUSE");
    /** no matching function for call to 'category_to_subcategory::get_subcat(const char[6]); */

    qDebug()<<"The const category is"<<cat_to_subcat_Instance->get_subcat("OS");
    qDebug()<<"The static const integer defined in class value is "<<category_to_subcategory::categ_const;
}

void function_op_finish()
{
    qDebug()<<__PRETTY_FUNCTION__;
}

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);
    function_to_QMAP();
    function_op_finish();
    return a.exec();
}
EN

回答 3

Stack Overflow用户

发布于 2014-03-03 20:24:13

看起来你混淆了基于堆栈的内存分配(局部变量在堆栈上分配,并在函数退出时自动销毁)和基于堆的内存分配(使用‘C++’分配,程序员负责确保在某个时刻调用'delete‘)。

下面是关于不同之处的friendly tutorial,欢迎使用C++手动管理内存(这可能是该语言中错误和问题的最大来源)。请注意,该链接特别指向C,而不是C++,但两者的概念是相同的。

票数 2
EN

Stack Overflow用户

发布于 2014-03-03 20:21:49

因为你不会delete cat_to_subcat_Instance。这是一个内存泄漏:对象被创建,但从未被销毁。

在您的特定代码片段中,我看到了动态分配cat_to_subcat_Instance的零需求。为什么不用简单的方法呢?

代码语言:javascript
复制
void function_to_QMAP()
{
    qDebug()<<__PRETTY_FUNCTION__;
    category_to_subcategory cat_to_subcat_Instance;
    QString cat = "OS";
    qDebug()<<cat_to_subcat_Instance.get_subcat(cat);
    cat = "SHELL";
    qDebug()<<cat_to_subcat_Instance.get_subcat(cat);
    cat = "MOUSE";
    qDebug()<<cat_to_subcat_Instance.get_subcat(cat);
    /** Passing just the string will throw an error **/
    //qDebug()<<cat_to_subcat_Instance.get_subcat("MOUSE");
    /** no matching function for call to 'category_to_subcategory::get_subcat(const char[6]); */

    qDebug()<<"The const category is"<<cat_to_subcat_Instance.get_subcat("OS");

    qDebug()<<"The static const integer defined in class value is "<<category_to_subcategory::categ_const;


}
票数 1
EN

Stack Overflow用户

发布于 2014-03-03 20:34:00

您使用运算符new在堆中分配了一个category_to_subcategory类型的对象。

代码语言:javascript
复制
category_to_subcategory *cat_to_subcat_Instance = new category_to_subcategory;

要删除此对象,需要显式调用operator delete

还要考虑到类category_to_subcategory的析构函数是无效的。

代码语言:javascript
复制
category_to_subcategory::~category_to_subcategory()
{
   qDebug()<<__PRETTY_FUNCTION__;
   delete this;
}

它的调用将导致析构函数的递归调用,直到堆栈内存耗尽。使用delete删除该语句。

代码语言:javascript
复制
category_to_subcategory::~category_to_subcategory()
{
   qDebug()<<__PRETTY_FUNCTION__;
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/22146634

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档