开发者

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

开发者 https://www.devze.com 2023-01-07 09:14 出处:网络
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.

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 :-)


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.


Well, your code is probably 100% legit. I have same issue but with Valgrind and also using boost::ptr_vector. Below is a prototype for test purpose:

#include <boost/ptr_container/ptr_vector.hpp>
#include <boost/shared_ptr.hpp>
#include <map>

using namespace std;
class Object
{
    public:
        Object(){ abc=1;};
        ~Object(){cout<<"destructor Object"<<endl;};
        int abc;
};

class test : public boost::ptr_vector<Object>
{
    public:

        test(){}
        virtual ~test(){cout<<"destructor test"<<endl;}
        void add(){
            PARENT::push_back(new Object());
        }
    protected:

        typedef boost::ptr_vector<Object> PARENT;
        test(const test& abc);
};

    typedef boost::shared_ptr<test> shared_type;
    typedef std::map< int, std::pair<shared_type, shared_type> > maptype;

    maptype::mapped_type get()
    {
        boost::shared_ptr<test> testObj(new test);
        boost::shared_ptr<test> testObj2(new test);

        test &ref= *(testObj.get());
        test &ref2= *(testObj2.get());

        ref.reserve(4);
        ref.add();
        ref.add();
        ref.add();
        ref.add();
        ref2.reserve(4);
        ref2.add();
        return maptype::mapped_type(testObj,testObj2);
    }

int main() {
    std::map< int, std::pair<shared_type, shared_type> > mapped;
    mapped.insert(maptype::value_type(1,get()));

    return 0;
}

I was testing some internal structure here so do not care about the design. The output is:

destructor test
destructor Object
destructor test
destructor Object
destructor Object
destructor Object
destructor Object

Valgrind can complain here also, i suppose its due to ptr_vector

0

精彩评论

暂无评论...
验证码 换一张
取 消