std::vector memory leak issues

Started by
11 comments, last by Crusable77 10 years, 5 months ago

Hello, I have an entity with a std::vector<Component*> m_components (Component is an abstract base class all other Components inherit from so I can polymorph them) and when I destroy the entity I clear the vector like so:


Entity::~Entity(){
	for(std::vector<Component*>::iterator iter = m_components.begin(); iter != m_components.end(); ++iter){
		delete *iter;
		*iter = NULL;
	}
	m_components.clear();
	m_components.shrink_to_fit();
} 

and I get memory leaks. I looked online and everywhere I look it looks like I am doing it right. The memory leaks are a jumble of numbers:


Detected memory leaks!
Dumping objects ->
{377} normal block at 0x05804D28, 8 bytes long.
 Data: < N      > 90 4E 80 05 00 00 00 00 
{376} normal block at 0x05804E80, 80 bytes long.
 Data: <  c   w   c     > 90 B1 63 05 D8 81 77 05 90 B1 63 05 00 00 CD CD 
{374} normal block at 0x05884028, 98304 bytes long.
 Data: <                > 00 00 FF FF 00 00 FF FF 00 00 FF FF 00 00 FF FF 
{373} normal block at 0x05804E38, 8 bytes long.
 Data: < M      > A4 4D 80 05 00 00 00 00 
{372} normal block at 0x05804DF0, 8 bytes long.
 Data: < M      > 80 4D 80 05 00 00 00 00 
{371} normal block at 0x05804D70, 68 bytes long.
 Data: <  c   w   c     > C8 B0 63 05 C8 80 77 05 C8 B0 63 05 00 00 CD CD 
{353} normal block at 0x05778268, 8 bytes long.
 Data: <  w     > E8 81 77 05 00 00 00 00 
{352} normal block at 0x057781D8, 80 bytes long.
 Data: <  c  *x  N      > 90 B1 63 05 10 2A 78 05 80 4E 80 05 01 00 CD CD 
{350} normal block at 0x057839A0, 262144 bytes long.
 Data: < $   $   $   $  > 83 24 0C FF 83 24 0C FF 83 24 0C FF 83 24 0C FF 
{349} normal block at 0x05778190, 8 bytes long.
 Data: <  w     > FC 80 77 05 00 00 00 00 
{348} normal block at 0x05778148, 8 bytes long.
 Data: <  w     > D8 80 77 05 00 00 00 00 
{347} normal block at 0x057780C8, 68 bytes long.
 Data: <  c   x pM      > C8 B0 63 05 08 19 78 05 70 4D 80 05 01 00 CD CD 
{326} normal block at 0x05772908, 8 bytes long.
 Data: < *x     > 20 2A 78 05 00 00 00 00 
{325} normal block at 0x05782A10, 80 bytes long.
 Data: < &w   c   w     > 18 26 77 05 90 B1 63 05 D8 81 77 05 01 00 CD CD 
{323} normal block at 0x057819D0, 4096 bytes long.
 Data: <                > FF 00 00 FF FF 00 00 FF FF 00 00 FF FF 00 00 FF 
{322} normal block at 0x05781988, 8 bytes long.
 Data: << x     > 3C 19 78 05 00 00 00 00 
{321} normal block at 0x05772950, 8 bytes long.
 Data: <  x     > 18 19 78 05 00 00 00 00 
{320} normal block at 0x05781908, 68 bytes long.
 Data: <@"w   c   w     > 40 22 77 05 C8 B0 63 05 C8 80 77 05 01 00 CD CD 
{303} normal block at 0x0577A100, 8 bytes long.
 Data: <(&w     > 28 26 77 05 00 00 00 00 
{302} normal block at 0x05772618, 80 bytes long.
 Data: <  c  *x   c     > 90 B1 63 05 10 2A 78 05 90 B1 63 05 01 00 CD CD 
{299} normal block at 0x05779030, 4096 bytes long.
 Data: <                > FF 00 00 FF FF 00 00 FF FF 00 00 FF FF 00 00 FF 
{298} normal block at 0x05772308, 8 bytes long.
 Data: <t"w     > 74 22 77 05 00 00 00 00 
{297} normal block at 0x057722C0, 8 bytes long.
 Data: <P"w     > 50 22 77 05 00 00 00 00 
{296} normal block at 0x05772240, 68 bytes long.
 Data: <  c   x   c     > C8 B0 63 05 08 19 78 05 C8 B0 63 05 01 00 CD CD 
{275} normal block at 0x0563B220, 8 bytes long.
 Data: <| c     > 7C B0 63 05 00 00 00 00 
{274} normal block at 0x0563B190, 80 bytes long.
 Data: < &w  *x  N      > 18 26 77 05 10 2A 78 05 80 4E 80 05 01 01 CD CD 
{273} normal block at 0x0563B148, 8 bytes long.
 Data: <p c     > 70 B0 63 05 00 00 00 00 
{272} normal block at 0x0563B0C8, 68 bytes long.
 Data: <@"w   x pM      > 40 22 77 05 08 19 78 05 70 4D 80 05 01 01 CD CD 
{271} normal block at 0x0563B070, 24 bytes long.
 Data: <H c   c       c > 48 B1 63 05 C8 B0 63 05 04 00 00 00 20 B2 63 05 
{171} normal block at 0x0357F378, 40 bytes long.
 Data: <  5_            > D4 C5 35 5F 18 00 00 00 08 00 00 00 00 00 00 00 
{170} normal block at 0x003FBFE8, 40 bytes long.
 Data: <  5_            > D4 C5 35 5F 18 00 00 00 08 00 00 00 00 00 00 00 
Object dump complete.

I am using VS2012's built in system for detecting memory leaks. This is the only place I am dynamically allocating anything in my program and when I comment out everything that has to do with the vector I get no errors. Thanks for any help.

Advertisement

You asked a similar question 2 days ago. I'll answer you the same way I answered in the other post:

- Read this article to learn how to correctly debug memory leaks.

- #define _CRTDBG_MAP_ALLOC at the beginning of the file(before the includes). For each leak, it will show where the allocation was made. This will make your life much easier.

Now, since you are using inheritance, but callind delete on the base class pointer, you need to make sure you use virtual destructors for all the classes(Base + derived). Otherwise, the destructors will not be called correctly.

Consider


class A
{
public:
    ~A();
};

class B : public A
{
public:
    ~B();
}

void foo()
{
    A* pA = new B;
    delete pA;
}

pA is a pointer to A, so when you call 'delete pA' it will call ~A() and not ~B(). By making ~A() and ~B() virtual destructors, you ensure that destructor that will be called is the object's one and not the pointer's one.

Hello, I did #define _CRTDBG_MAP_ALLOC and my destructors are virtual:


/*Component is the parent to all other components
so the entity class can have a std::vector<Componet*>*/

#ifndef COMPONENT_HPP
#define COMPONENT_HPP

class Component{
public:
	Component();
	virtual ~Component() = 0;
	virtual void update(float deltaT);
};

#endif //COMPONENT_HPP

Dude, read the MSDN article, it will tell exactly how to debug memory leaks, including pinpointing and breaking on the allocation.

I did read the article and I did everything it told me to and I still don't get the full debug log.

There's a section there on how to interpret the leaking allocation number, and how to break on those when the allocations are made. Did you try using it?

The bigger question is whether or not the class being delete is also allocating memory. Also like mentioned before, you should be deleting reference to the concrete instances and not the base class.

Use `std::vector<std::unique_ptr<Component>>' and be done with it.

Also, you need to understand about how std::allocator works as a caching pool allocator that may (depending on the implementation) use memory of dynamic storage duration which is freed after return from main() or possibly not at all. This is by design. That means that it may be normal to see what appear to be leaks when using containers from the standard library, especially if you're not using the right tools or not using the tools right.

Stephen M. Webb
Professional Free Software Developer

Do what N.I.B. says. (+1)

You're vector delete code looks ok to me (making sure Components and derived classes have a virtual destructor). So I have to ask if it's plugged in?

Are you dynamically allocating any Entity's? If so, you'll need to delete them obviously. Put a break point in your destructor to be sure it's getting called.

Plus any components that new up any objects, need to delete those as well.

- Eck

EckTech Games - Games and Unity Assets I'm working on
Still Flying - My GameDev journal
The Shilwulf Dynasty - Campaign notes for my Rogue Trader RPG

This topic is closed to new replies.

Advertisement