c++boostmemory-leaksptr-vector

ptr_vector - _CrtDumpMemoryLeaks() - memory leaks even though destructor is called


I'm working on a game engine and in an earlier question it was suggested that I start using boost::ptr_vector to maintain a list of pointers.

The basic idea is to have several State's, each State has a SceneGraph. Each state has several resources that they initialize, and then stuff its own SceneGraph. The SceneGraph has a boost::ptr_vector that it stuffs the resource pointers in.

Here's the relevant code:

Resource creation and addition to the SceneGraph in TestState

backgroundImage = new DEBUG_NEW Fenris::Node2D::Image(std::string("backgroundImage"), std::string("../media/img/background.jpg"));
sceneGraph->addNode(backgroundImage, Fenris::Core::STRATUM_BACK);

SceneGraph

boost::ptr_vector<SceneGraphNode> backStratumList;
// The add() method
void addNode(SceneGraphNode *pNode, STRATUM_TYPE stratumType) { switch(stratumType) { case STRATUM_BACK: backStratumList.push_back(pNode); break; case STRATUM_NORMAL: normalStratumList.push_back(pNode); break; case STRATUM_FOREGROUND: foregroundStratumList.push_back(pNode); break; } }

Edited main.cpp with relevant lines

PlatformGame::State::TestState *testState = new DEBUG_NEW PlatformGame::State::TestState(std::string("testState"));
// Clean up the previously registered state (deletes its sceneGraph -- verified that the destructor is in fact called via debugger) delete testState;
// Dump memleak report if we're running in debug mode #ifdef _DEBUG _CrtDumpMemoryLeaks(); #endif

I'm using _CrtDumpMemoryLeaks() to output a memory leak log report. The log report tells me I have a memory leak;

Detected memory leaks!
Dumping objects ->
{174} normal block at 0x00A56630, 32 bytes long.
 Data: <../media/img/bac> 2E 2E 2F 6D 65 64 69 61 2F 69 6D 67 2F 62 61 63 
{173} normal block at 0x00A565A0, 8 bytes long.
 Data: < c      > A8 63 A5 00 00 00 00 00 
Object dump complete.

Is _CrtDumpMemoryLeaks() having trouble with boost::ptr_vector or have I done something wrong? The debugger tells me that State does invoke its destructor (which has; delete sceneGraph) and I've also verified that the SceneGraph's destructor is also invoked.

Any help is greatly appreciated, I'd love to see an empty memory leak report :-)


Solution

  • It sure doesn't look like a vector you're leaking. Note that the string is readable, that's at least one hint.

    If you can get the number between the curly braces stable ("{173}") then you can get a breakpoint when the memory is allocated. Put this in your main() function:

    _crtBreakAlloc = 173;
    

    Use #include <crtdbg.h> if necessary. Repeat for 174 to find the other one.