c++ - 好与坏:在构造函数中调用析构函数
问题描述
Break:我认为这实际上不是同一个问题,另一个问题是关于手动调用析构函数的一般问题。这是在类本身内部的创建过程中。仍然想知道这样做时会发生什么,如下面的问题所述。
起初,我认为这很糟糕,真的很糟糕。只需分析这段由两个人制作的构造函数代码(见下文),需要将其转换为 Delphi 对象 Pascal。它的行为必须与 C 版本相同。我不喜欢这种风格,很丑,但没关系。
另一件事,在代码的两个阶段,它在失败时调用析构函数(我想关闭连接,但是在删除时会自动调用析构函数,为什么还要这样做?)。我认为这不是这样做的方法,还是确实错过了一些东西?
此外,在调用析构函数后,他们想抛出一个异常(嗯?)但是我认为这永远不会被执行,并且当您手动想要访问它或想要删除它时会导致另一个异常。
Serial::Serial(
std::string &commPortName,
int bitRate,
bool testOnStartup,
bool cycleDtrOnStartup
) {
std::wstring com_name_ws = s2ws(commPortName);
commHandle =
CreateFileW(
com_name_ws.c_str(),
GENERIC_READ | GENERIC_WRITE,
0,
NULL,
OPEN_EXISTING,
0,
NULL
);
if(commHandle == INVALID_HANDLE_VALUE)
throw("ERROR: Could not open com port");
else {
// set timeouts
COMMTIMEOUTS timeouts;
/* Blocking:
timeouts.ReadIntervalTimeout = MAXDWORD;
timeouts.ReadTotalTimeoutConstant = 0;
timeouts.ReadTotalTimeoutMultiplier = 0;
Non-blocking:
timeouts = { MAXDWORD, 0, 0, 0, 0}; */
// Non-blocking with short timeouts
timeouts.ReadIntervalTimeout = 1;
timeouts.ReadTotalTimeoutMultiplier = 1;
timeouts.ReadTotalTimeoutConstant = 1;
timeouts.WriteTotalTimeoutMultiplier = 1;
timeouts.WriteTotalTimeoutConstant = 1;
DCB dcb;
if(!SetCommTimeouts(commHandle, &timeouts)) {
Serial::~Serial(); <- Calls destructor!
throw("ERROR: Could not set com port time-outs");
}
// set DCB; disabling harware flow control; setting 1N8 mode
memset(&dcb, 0, sizeof(dcb));
dcb.DCBlength = sizeof(dcb);
dcb.BaudRate = bitRate;
dcb.fBinary = 1;
dcb.fDtrControl = DTR_CONTROL_DISABLE;
dcb.fRtsControl = RTS_CONTROL_DISABLE;
dcb.Parity = NOPARITY;
dcb.StopBits = ONESTOPBIT;
dcb.ByteSize = 8;
if(!SetCommState(commHandle, &dcb)) {
Serial::~Serial(); <- Calls destructor!
throw("ERROR: Could not set com port parameters");
}
}
if(cycleDtrOnStartup) {
if(!EscapeCommFunction(commHandle, CLRDTR))
throw("ERROR: clearing DTR");
Sleep(200);
if(!EscapeCommFunction(commHandle, SETDTR))
throw("ERROR: setting DTR");
}
if(testOnStartup) {
DWORD numWritten;
char init[] = "PJON-python init";
if(!WriteFile(commHandle, init, sizeof(init), &numWritten, NULL))
throw("writing initial data to port failed");
if(numWritten != sizeof(init))
throw("ERROR: not all test data written to port");
}
};
Serial::~Serial() {
CloseHandle(commHandle);
};
// and there is more etc .......
// .............
下一个问题,当执行这段代码并调用析构函数时,内存中实际会发生什么?我无法执行和调试它。
解决方案
这段代码丑陋但合法。当构造函数抛出异常时,永远不会调用相应的析构函数。所以需要在抛出前手动调用,防止资源泄露。这里真正的错误是在抛出异常之前在其他情况下没有手动调用析构函数。
当然,更好的方法是使用一个单独的 RAII 对象来封装commHandle
. 具有自定义删除器的 Aunique_ptr
可以充当此角色。
任何超出低级库的析构函数都是现代 C++ 中的代码异味。
推荐阅读
- node.js - MEAN Stack:模块 .exports 不是函数
- openstack - Javaswift/JOSS 中的 StoredObjectListElement 与 openstack swift API 不匹配?
- php - PHP搜索功能,用于搜索不精确搜索词的数据库
- ibm-cloud-infrastructure - SL API:数据中心与优惠之间的纽带
- azure - 在 chef-solo 中使用环境进行打包
- c# - C#:覆盖现有类的运算符
- highcharts - 仅在调整窗口大小后才显示条形图上的堆栈标签
- javascript - 如何在猫鼬的一条记录中对对象数组进行排序
- java - 挂起两个侦听器的 Java 套接字应用程序
- r - 读取csv文件后如何将因子类型转换为R中的数字类型?