为什么没有调用我的析构函数
Why is my destructor not being called?
程序
退出时没有调用我的析构函数。 对象是单例,也许我错过了什么?
这是标头和 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;
}
}
您的析构函数没有被调用,因为没有人为使用 new
创建的对象调用delete
:
mysqlConnector = new MysqlConnector(); // Where's the corresponding call to delete?
您可以考虑使用智能指针代替(如果您负担得起C++11,我建议std::unique_ptr
(。当智能指针本身被销毁时,这将在封装对象上自动调用delete
。
另一种可能性是根本不使用指针,并具有类型 MysqlConnector
的静态数据成员。 然后,getInstance()
可以返回对该对象的引用(而不是指针(。
static MysqlConnector* mysqlConnector;
指向类的指针。当您获取实例时,它只返回此指针。您需要在此静态指针上调用 delete
才能调用析构函数。
如果您继续在此指针上调用 delete,请确保仅在程序终止时才这样做 - 因为下一次调用 getInstance()
将创建一个新对象。
编辑 - 刚刚发现了 instanceFlag,因此它不会创建新实例,但会返回一个 NULL 指针
这是有问题的,因为您可能希望 Singleton 是同一个对象,但当然创建它的新实例将意味着您的数据不会持久(即使您正在访问单例(。
无论如何,操作系统都会回收内存,因此您最好公开连接关闭代码并自行调用?
相关文章:
- 为什么没有捕获我来自析构函数的异常
- 如何在关闭Qt控制台应用程序之前运行我的析构函数?
- 为什么我的析构函数中的异常没有触发 std::终止?
- 为什么我的析构函数被调用多次
- 为什么我的析构函数被调用的次数比预期的要多
- 我是否应该假设对象的析构函数在从 STL 容器中删除后立即调用?
- 我是否正确构建了这些引用和指针函数?如果是,我该如何显式调用我的析构函数
- 为什么我的析构函数导致我的链表出现问题
- 在这种情况下,我是否需要调用矢量的析构函数
- 我无法让我的析构函数在我的代码中工作
- 我的析构函数需要在这个函数中是什么
- boost::ptr_vector with arrays:我能确定它的析构函数调用delete[]而不是delete吗
- 为什么从 win32 计时器回调抛出时不调用我的析构函数?
- 为什么没有调用我的析构函数
- 为什么gcc链接器看不到我的析构函数
- 为什么我的析构函数被调用两次
- 为什么我的析构函数没有破坏我的对象,它仍然保留其成员变量值
- 我的析构函数每隔一秒调用一次
- 为什么我的析构函数被调用,我如何修复它
- 如何正确删除ptrs数组?我的析构函数似乎丢失了实际的对象