2014-03-03 101 views
0

我做了一個實例化類的函數,並做了一些操作。之後,函數返回,這意味着範圍結束。一旦範圍結束後,它應該調用該類的析構函數。但是,我沒有看到析構函數被調用。它背後的原因是什麼?我在這裏錯過了什麼嗎?爲什麼在這個程序中不調用析構函數?

/*** 

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(); 
} 
+0

你爲什麼要粘貼X頁的代碼,而不是告訴我們你正在談論的是什麼功能? – quetzalcoatl

+3

把'delete this'放在析構函數中永遠都不是正確的事情。 'delete'調用析構函數,而不是相反。 – Sneftel

回答

1

因爲你沒有deletecat_to_subcat_Instance。這是一個內存泄漏:對象被創建,但從未銷燬。

在您的特定代碼片段中,我看到動態分配cat_to_subcat_Instance的零需求。爲什麼不這麼簡單呢?

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; 


} 
+0

@SHREYASJOSHI:因爲這不是_dynamic_ allocation的工作方式。但這就是堆棧分配的工作原理。查看我的編輯答案,瞭解如何在堆棧上分配的示例。 –

+0

@SHREYASJOSHI:變量是一個指向對象的指針。變量在超出範圍時被銷燬。所以,**指針**在超出範圍時被銷燬。它指向的對象不會被銷燬。如果你想使用一個'指針式的東西',也會自動銷燬對象 - 使用智能指針。 – quetzalcoatl

2

似乎你混淆了兩種類型的C++內存分配 - 基於堆棧(局部變量分配在堆棧上,並自動退出函數時破壞)使用「新」和分配和基於堆的(程序員負責確保'刪除'在某些時候被調用)

這裏是一個關於friendly tutorial的區別,並且歡迎使用C++手動管理內存的悲歡離合(這可能是錯誤和問題的最大來源在語言中)。請注意,鏈接特指C而不是C++,但概念在兩者中都是相同的。

0

您使用運算符new在堆中分配了category_to_subcategory類型的對象。

category_to_subcategory *cat_to_subcat_Instance = new category_to_subcategory; 

要刪除此對象,您需要明確地調用運算符delete

還要考慮到類category_to_subcategory的析構函數無效。

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

它調用將導致析構函數的遞歸調用,直到堆棧內存將被耗盡。 刪除語句。

category_to_subcategory::~category_to_subcategory() 
{ 
    qDebug()<<__PRETTY_FUNCTION__; 
} 
相關問題