为什么我的析构函数没有被调用?

时间:2013-05-23 15:21:39

标签: c++ oop destructor

程序退出时,我的析构函数未被调用。对象是单身,也许我错过了什么?

这是标题和cpp文件:

#ifndef MYSQLCONNECTOR_H
#define MYSQLCONNECTOR_H

/* Standard C++ headers */
#include <iostream>
#include <string>

/* MySQL Connector/C++ specific headers */
#include <driver.h>
#include <connection.h>
#include <statement.h>
#include <prepared_statement.h>
#include <resultset.h>
#include <metadata.h>
#include <resultset_metadata.h>
#include <exception.h>
#include <warning.h>

class MysqlConnector {
private:
    static bool instanceFlag;
    static MysqlConnector* mysqlConnector;

    MysqlConnector() {

    };
public:
    static sql::Driver *driver;
    static sql::Connection *conn;
    static MysqlConnector* getInstance();

    virtual ~MysqlConnector() {
        instanceFlag = false;
        conn->close();
        delete conn;
        std::cout << "called" << std::endl;
    };
private:

};

#endif  /* MYSQLCONNECTOR_H */

和cpp文件

#include "MysqlConnector.h"

using namespace std;
using namespace sql;

bool MysqlConnector::instanceFlag = false;
MysqlConnector* MysqlConnector::mysqlConnector = NULL;

MysqlConnector* MysqlConnector::getInstance() {
    if (!instanceFlag) {
        mysqlConnector = new MysqlConnector();
        instanceFlag = true;

        try {
            driver = get_driver_instance();

            /* create a database connection using the Driver */
            conn = driver->connect("tcp://127.0.0.1:3306", "root", "root");

            /* turn off the autocommit */
            conn -> setAutoCommit(0);

            /* select appropriate database schema */
            conn -> setSchema("exchange");

        } catch (SQLException &e) {
            cout << "ERROR: SQLException in " << __FILE__;
            cout << " (" << __func__ << ") on line " << __LINE__ << endl;
            cout << "ERROR: " << e.what();
            cout << " (MySQL error code: " << e.getErrorCode();
            cout << ", SQLState: " << e.getSQLState() << ")" << endl;

            if (e.getErrorCode() == 1047) {
                cout << "\nYour server does not seem to support Prepared Statements at all. ";
                cout << "Perhaps MYSQL < 4.1?" << endl;
            }
        } catch (std::runtime_error &e) {
            cout << "ERROR: runtime_error in " << __FILE__;
            cout << " (" << __func__ << ") on line " << __LINE__ << endl;
            cout << "ERROR: " << e.what() << endl;
        }

        return mysqlConnector;
    } else {
        return mysqlConnector;
    }
}

2 个答案:

答案 0 :(得分:7)

您的析构函数未被调用,因为没有人为delete创建的对象调用new

mysqlConnector = new MysqlConnector(); // Where's the corresponding call to delete?

您可以考虑使用智能指针(我建议std::unique_ptr,如果您能负担得起C ++ 11)。当智能指针本身被破坏时,这将在封装对象上自动调用delete

另一种可能性是根本不使用指针,并且具有类型为MysqlConnector的静态数据成员。然后getInstance()可以返回该对象的引用(而不是指针)。

答案 1 :(得分:0)

static MysqlConnector* mysqlConnector;

是指向类的指针。当你得到实例时,它只返回这个指针。您需要在此静态指针上调用delete以便调用析构函数。

如果您继续并在此指针上调用delete,请确保仅在程序终止时执行此操作 - 因为下次调用getInstance()将创建一个新对象。

编辑 - 刚发现instanceFlag所以它不会创建一个新实例但会返回一个NULL指针

这是有问题的,因为您可能希望Singleton是同一个对象,但是当然创建它的新实例将意味着您的数据将不会持久(即使您正在访问单例)。

操作系统会回收内存,所以你最好公开连接关闭代码并自己调用它?