Search code examples
c++boostmemory-leaksboost-thread

CRT Debug Heap reports leak when including exception_ptr.hpp


I created a Win32 Console application with Visual Studio 2013 (vc12) and Boost 1.56.0.

This is my only file:

#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;
}

When I run this (Win32/Debug on Windows 7) I get the following output in 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.

I first discovered this issue in a unit test which included boost/thread.hpp. However the unit tests don't include source and line information about the leak and thread.hpp doesn't compile with the DEBUG_CLIENTBLOCK macro. So I started eliminating all includes until I found out that the one causing the reported leak was exception_ptr and luckily enough this one did compile with the macro.

I opened a ticket to Boost about it (https://svn.boost.org/trac/boost/ticket/10621) but I thought I'd try here as well. I guess it's also possible I'm doing something wrong.


Solution

  • Like I mentioned in the question, this is a bug in Boost. You can see the ticket I opened here: https://svn.boost.org/trac/boost/ticket/10621

    In the meantime, since we moved to VC12, we started using std::thread, so in my sepcific case, it's not a problem any more.