调用C++析构函数失败
Call in C++ Destructor Fails
我有这个代码:
DManag::~DManag() {
printf("in destn");
if(mainConn.IsOpen()) {
printf("about to closen");
mainConn.Close();
printf("closed!n");
}
printf("end destn");
}
输出为:in dest n about to close n
,仅此而已。
它(mainConn->CDatabase类)在调用close()时似乎无声地失败了。我知道你应该在结束关系后立即关闭关系。但我从其他人那里继承了这段代码,但该程序并没有提供在正确的时间关闭连接的简单方法。Close()和open()调用位于afxdb.h
中。
知道它为什么会这样失败吗?谢谢
问题似乎是CDatabase::Close
抛出异常并导致析构函数的其余部分被绕过。文档中没有提到这个方法可以抛出,但互联网上的其他使用示例表明它可以。
- http://www.codeproject.com/KB/database/readdb.aspx
- http://www.dreamincode.net/forums/topic/211232-cdatabaseopenex-fails-without-exception-in-release-mode/
尝试按以下修改析构函数
DManag::~DManag() {
printf("in destn");
if(mainConn.IsOpen()) {
printf("about to closen");
try {
mainConn.Close();
} catch (CDBException&) {
print("exception occurredn");
}
printf("closed!n");
}
printf("end destn");
}
相关文章:
- 什么时候调用组成单元对象的析构函数
- 如果C++类在类方法中具有动态分配,但没有构造函数/析构函数或任何非静态成员,那么它仍然是POD类型吗
- 内联映射初始化的动态atexit析构函数崩溃
- 什么时候调用析构函数
- 优先顺序:智能指针和类析构函数
- C++-明确何时以及如何调用析构函数
- 使用基类指针创建对象时,缺少派生类析构函数
- 在c++中使用向量时,如何调用构造函数和析构函数
- 重载运算符new[]的行为取决于析构函数
- 我需要知道编译器如何在cpp中使用析构函数
- 为什么在使用转换构造函数赋值后调用C++类的析构函数?
- 析构函数调用
- 在VS 2017中,朋友通过具有私有析构函数的结构的unique_ptr向量进行迭代失败
- SDL在析构函数上释放曲面失败
- 虚拟析构函数测试失败
- CUDA 调用在析构函数中失败
- 重载的*运算符在多个*操作后调用析构函数时失败
- 在链表析构函数(C++Visual Studio)上调试断言失败
- 调用C++析构函数失败
- 如何处理析构函数中互斥对象销毁失败的问题