Skip to main content
GameDev.net gamedev.net
🔒 Locked

getting MMGR to ignore std::list

Started by Nit Jan 26, 2006 at 8:21 AM 5 replies 1.9k views
Original Post
Nit
Nit
I recently posted about a memory leak that was detected by MMGR, that was the result of std::list usage of a memory pool. So knowing that this wasn't really a memory leak, someone (i think it was Sneftel, thanks!) pointed out that I should just include nommgr.h before my standard includes, and mmgr.h afterwards. Well, that seemed simple enough, however I seem to still get the std::list "leak" reported by mmgr. So my question is: what is the proper way to integrate mmgr into a project. Before you say "RTFM", I would like to add that I did Read The Finger-licking-good Manual, but I am still having troubles. I understand that I should order my includes as follows: standard includes mmgr.h project includes There are a few questions about this though. Does this require me to have all of my includes in the .h files? Sometimes I throw an include in the .cpp when the .cpp is using something that is internal to that class, and doesn't need to be accessible to other classes that includes this file. Or should I have an "engine.h" like what is found in the Enginuity series here on gamedev, and include mmgr.h in that file? I have included a small working example of a possible use of mmgr, but this mmgr will report a leak allocated by "??(00000)::??". If i put a break point in the mmgr's new operator overload, I find that this is allocation originates from std::list. See for yourself: "header1.h"

#ifndef HEADER1_TEST_H__
#define HEADER1_TEST_H__

#include "nommgr.h"
#include <iostream>
#include <list>
#include "mmgr.h"

class Foo
{
private:
    std::list<std::string> stringList;
public:
    Foo() {}
    ~Foo() {}

};

#endif


"main.cpp"

#include "header1.h"

int main(void)
{
    Foo* foo = new Foo();

    return 0;
}


I would appreciate any helpful feedback on this matter. Thanks. [EDIT] To clarify, I know why mmgr thinks this is a memory leak (see link) But I would like to know how I can organize my project so that mmgr does not report this as a leak.
Puzzler183
Puzzler183
It's reporting it as a memory leak because that's what your program does: leak memory. Your objects destructor is never called so I'm pretty sure the std::list destructor is never called, hence it will leak memory. Does it still report it if you put a delete foo; in there?
Nit
Nit
Well, this was a small isolated example that I was hoping to use that would illustrate what I am seeing in my larger project. I know that in this exampale, "Foo* foo = new Foo()" is a leak, but it shouldn't be reporting foo's list as a leak, since I am trying to get mmgr to ignore stl.

EDIT:
So in this example mmgr reports 2 leakes, 1 in main and one in std::list. I would like it to only report 1 leak.

In the meantime, I will try to come up with a better example, where I delete foo but the std::list is still reported as a leak (this is what I am seeing in my larger project).
DrEvil
DrEvil
One idea for why it might not be working(and I could be way off) is because since list, and most other STL classes are templates, most of their functions get inlined. It would seem that this could mean that the constructor is being inlined into main, which is being effected by the memory manager.

This is probably not right tho, as I'd think that the memory manager preprocessor replacements for new/delete would be done before any inlining, so that even inlined news from the list header would be the normal new, and not the memory manager new.
Nit
Nit
Ah ha!!

EDIT
Don't be intimidated by all of the source blocks. This code is ridiculously simple and small.
END EDIT

Here is exactly my problem that I am seeing in my large project, the Foos are properly deleted, but the lists are still reported as leaks.

"main.cpp"
#include "header1.h"int main(void){    Foo* foo = new Foo();    Foo* foo2 = new Foo();    Foo* foo3 = new Foo();    foo2->addChild(foo3);    foo->addChild(foo2);    foo->destroy();    delete foo;    return 0;}


"header1.h"
#ifndef HEADER1_TEST_H__#define HEADER1_TEST_H__#include "nommgr.h"#include <iostream>#include <list>#include "mmgr.h"#include "header2.h"class Foo : public CObject{private:    std::list<std::string> stringList;public:    Foo() {}    ~Foo() {}};#endif


"header2.h"
#ifndef HEADER2_H__#define HEADER2_H__#include "nommgr.h"#include <list>#include "mmgr.h"class CObject{private:    std::list<CObject*> children;public:    CObject() {}    ~CObject() {}    virtual void destroy();    virtual void addChild(CObject* child);};#endif


"source2.cpp"
#include "header2.h"void CObject::destroy(){    for(std::list<CObject*>::iterator it=children.begin(),end=children.end();         it != end; it++)    {	(*it)->destroy();	delete *it;    }}void CObject::addChild(CObject* child){    children.push_back(child);}


MMGR's output:
 ---------------------------------------------------------------------------------------------------------------------------------- |                                          Memory leak report for:  01/26/2006 11:29:36                                            | ---------------------------------------------------------------------------------------------------------------------------------- 2 memory leaks found:Alloc.   Addr       Size       Addr       Size                        BreakOn BreakOn              Number Reported   Reported    Actual     Actual     Unused    Method  Dealloc Realloc Allocated by ------ ---------- ---------- ---------- ---------- ---------- -------- ------- ------- --------------------------------------------------- 000006 0x0032B8C0 0x00000024 0x0032B8B0 0x00000044 0x0000001C new         N       N    ??(00000)::??000009 0x0032BA20 0x00000024 0x0032BA10 0x00000044 0x0000001C new         N       N    ??(00000)::??


Again, is there any way to prevent mmgr from reporting false leaks in std::list?
Enigma
Enigma
And the leaks are because you invoked delete on a base class pointer to a derived class object where the base class destructor is not virtual. Make the CObject destructor virtual and everything will work fine. You are leaking memory currently (actually, it's worse than that. I'm pretty sure you have undefined behaviour, but don't have easy access to the final draft standard to check).

Enigma
Nit
Nit
Enigma, you hit the nail on the head! I thought that the other list was leaking, due to memory pool allocation. Making CObjects destructor virtual fixed this whole mess. I can't believe I missed that. Well, big thanks to Enigma and big thanks to Paul Nettle for making a solid memory manager that can catch mistakes like this.

Problem solved. Thank you.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.