CRT调试堆在包含exception_ptr.hpp时报告泄漏

CRT Debug Heap reports leak when including exception_ptr.hpp

本文关键字:hpp ptr 报告 泄漏 exception 调试 包含 CRT      更新时间:2023-10-16

我使用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;
}

当我运行这个(Win32/Debug在Windows7上)时,我在MemoryLeaks.txt中得到以下输出:

Detected memory leaks!
Dumping objects ->
c:workspaceexternalsboost_1_56_0includeboostsmart_ptrdetailshared_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:workspaceexternalsboost_1_56_0includeboostexceptiondetailexception_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:workspaceexternalsboost_1_56_0includeboostsmart_ptrdetailshared_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:workspaceexternalsboost_1_56_0includeboostexceptiondetailexception_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宏编译。所以我开始消除所有的include,直到我发现导致报告泄漏的是exception_ptr,幸运的是,这个包确实用宏编译了。

我开了一张票给Boost(https://svn.boost.org/trac/boost/ticket/10621)但我想我也可以在这里试试。我想我也有可能做错了什么。

就像我在问题中提到的,这是Boost中的一个bug。你可以在这里看到我打开的票:https://svn.boost.org/trac/boost/ticket/10621

同时,由于我们转移到了VC12,我们开始使用std::thread,所以在我的具体情况下,这不再是一个问题。