【发布时间】:2023-03-06 03:45:01
【问题描述】:
我使用 Visual Studio 2013 (vc12) 和 Boost 1.56.0 创建了一个 Win32 控制台应用程序。
这是我唯一的文件:
#define _CRTDBG_MAP_ALLOC
#include <stdlib.h>
#include <crtdbg.h>
//#define CHECK_THREAD
#ifdef CHECK_THREAD
#include <boost/thread.hpp>
#else
#ifdef _DEBUG
#define DEBUG_CLIENTBLOCK new( _CLIENT_BLOCK, __FILE__, __LINE__)
#else
#define DEBUG_CLIENTBLOCK
#endif // _DEBUG
#ifdef _DEBUG
#define new DEBUG_CLIENTBLOCK
#endif
#include <boost/exception/detail/exception_ptr.hpp>
#endif
#include <Windows.h>
int main()
{
HANDLE hLogFile = CreateFile(L"MemoryLeaks.txt", GENERIC_WRITE, FILE_SHARE_WRITE,
NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
//Turn on debugging for memory leaks. This is automatically turned off when the build is Release.
_CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF);
_CrtSetReportMode(_CRT_WARN, _CRTDBG_MODE_FILE);
_CrtSetReportFile(_CRT_WARN, hLogFile);
_CrtSetReportMode(_CRT_ERROR, _CRTDBG_MODE_FILE);
_CrtSetReportFile(_CRT_ERROR, hLogFile);
_CrtSetReportMode(_CRT_ASSERT, _CRTDBG_MODE_FILE);
_CrtSetReportFile(_CRT_ASSERT, hLogFile);
_CrtDumpMemoryLeaks();
CloseHandle(hLogFile);
return 0;
}
当我运行它(Windows 7 上的 Win32/Debug)时,我在 MemoryLeaks.txt 中得到以下输出:
Detected memory leaks!
Dumping objects ->
c:\workspace\externals\boost_1_56_0\include\boost\smart_ptr\detail\shared_count.hpp(130) : {156} client block at 0x00709AC8, subtype 0, 16 bytes long.
Data: < 0 p > 84 0F 0C 00 02 00 00 00 01 00 00 00 30 9A 70 00
{155} normal block at 0x00709A88, 14 bytes long.
Data: <bad exception > 62 61 64 20 65 78 63 65 70 74 69 6F 6E 00
c:\workspace\externals\boost_1_56_0\include\boost\exception\detail\exception_ptr.hpp(130) : {154} client block at 0x00709A30, subtype 0, 44 bytes long.
Data: < @ > 04 0E 0C 00 00 00 00 00 40 0E 0C 00 F0 0C 0C 00
c:\workspace\externals\boost_1_56_0\include\boost\smart_ptr\detail\shared_count.hpp(130) : {151} client block at 0x007089B0, subtype 0, 16 bytes long.
Data: <h X p > 68 0F 0C 00 02 00 00 00 01 00 00 00 58 89 70 00
c:\workspace\externals\boost_1_56_0\include\boost\exception\detail\exception_ptr.hpp(130) : {150} client block at 0x00708958, subtype 0, 44 bytes long.
Data: < X > B4 0C 0C 00 00 00 00 00 58 0D 0C 00 F0 0C 0C 00
Object dump complete.
我首先在包含 boost/thread.hpp 的单元测试中发现了这个问题。但是,单元测试不包括有关泄漏的源代码和行信息,并且 thread.hpp 不能使用 DEBUG_CLIENTBLOCK 宏进行编译。所以我开始消除所有包含,直到我发现导致报告泄漏的是 exception_ptr 并且幸运的是这个确实使用宏编译。
我为此向 Boost 开了一张票 (https://svn.boost.org/trac/boost/ticket/10621),但我想我也会在这里尝试。我想我也有可能做错了什么。
【问题讨论】:
-
+1 表示努力。我希望这是预期的行为,因为堆报告是在
main()内部触发的。不过,如果有一个 MSVC buff 加入进来就好了。 -
请不要在没有
#define WIN32_LEAN_AND_MEAN和#define NOMINMAX的情况下使用#include <Windows.h>Windows.h。 -
@sehe 就像我说的,我最初的问题是我有一个单元测试。我现在正在将我的项目升级到最新的 protobuf 和最新的 VC12 而不是 VC10 的提升,在所有这些升级之后,单元测试仍然通过,但是 Boost 报告了泄漏。通过相当多的消除,我设法找到了根本原因。在所有升级之前,没有报告泄漏。 (感谢您的 +1 :-))
标签: c++ boost memory-leaks boost-thread