【发布时间】:2018-01-03 11:40:36
【问题描述】:
我正在创建一个串行通信库,它是一个托管程序集,带有它自己的规则(我不完全理解它们,我只是在 VS 抱怨时更改代码),并且我有一个内存泄漏我想不通。
这是我得到的泄漏警告(第 11 行是 SerialComm::SerialComm() 中的 InfoContainer 构造函数):
Detected memory leaks!
Dumping objects ->
SerialComm.cpp(11) : {144} normal block at 0x011468B8, 56 bytes long.
Data: < > 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
Object dump complete.
这是 lib 的 .h 文件:
#include <windows.h>
#include <stdlib.h>
#include <atlstr.h>
public class InfoContainer {
public:
~InfoContainer();
HANDLE handle;
bool connected;
COMSTAT status;
DWORD errors;
DCB connection_params;
CStringA port_name;
};
public ref class SerialComm {
public:
InfoContainer* info=0;
SerialComm();
~SerialComm();
bool OpenConnection(String^ Portname);
int CloseConnection();
bool WriteData(String^ toSend);
String^ ReadData(int bytesToRead);
};
这些是相关的 .cpp 部分:
SerialComm::SerialComm() {
info = new (_NORMAL_BLOCK, __FILE__, __LINE__) InfoContainer();
info->handle = 0;
}
SerialComm::~SerialComm() {
CloseConnection();
delete info;
}
bool SerialComm::OpenConnection(String ^ Portname) {
info->port_name = Portname;
//visual studio's valgrindish tool
_CrtSetReportMode(_CRT_WARN, _CRTDBG_MODE_FILE);
_CrtSetReportFile(_CRT_WARN, _CRTDBG_FILE_STDOUT);
_CrtSetReportMode(_CRT_ERROR, _CRTDBG_MODE_FILE);
_CrtSetReportFile(_CRT_ERROR, _CRTDBG_FILE_STDOUT);
_CrtSetReportMode(_CRT_ASSERT, _CRTDBG_MODE_FILE);
_CrtSetReportFile(_CRT_ASSERT, _CRTDBG_FILE_STDOUT);
_CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF);
_CrtDumpMemoryLeaks();
info->handle = CreateFileA(
info->port_name,
GENERIC_READ | GENERIC_WRITE,
0,
NULL,
OPEN_EXISTING,
0,
NULL
);
if (info->handle == INVALID_HANDLE_VALUE) {
ATLTRACE("Error: %d", GetLastError());
printf("Error: %d\n", GetLastError());
return false;
}
//
//rest of connection code here
//
}
int SerialComm::CloseConnection() {
return CloseHandle(info->handle);
}
InfoContainer::~InfoContainer() {
delete handle;
}
我必须在主类中使用 InfoContainer 类和 InfoContainer 指针的原因是我需要存储的一些信息被认为是非托管代码,因此我不能直接在主类中拥有它。
提前致谢!
【问题讨论】:
-
在 VS Watch Window 中写入值 = -1 的
_crtBreakAlloc。现在将其值设置为 144。那么 VS 应该在第 144 次内存分配时中断。
标签: windows visual-studio visual-c++ c++-cli managed-c++