Jump to content

  • Log In with Google      Sign In   
  • Create Account


std::vector memory leak issues


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
12 replies to this topic

#1 Crusable   Members   -  Reputation: 590

Like
0Likes
Like

Posted 12 November 2013 - 02:57 AM

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.


"C spilled his beer all over C++'s shirt. Outraged, C++ shouted, "Good god, man! Have you no class?"

"Your mother is so fat that the recursive function that was used to calculate her mass created a stack overflow"

 

 


Sponsor:

#2 N.I.B.   Members   -  Reputation: 1021

Like
6Likes
Like

Posted 12 November 2013 - 03:12 AM

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.


Edited by N.I.B., 12 November 2013 - 03:15 AM.


#3 Crusable   Members   -  Reputation: 590

Like
0Likes
Like

Posted 12 November 2013 - 05:12 AM

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

Edited by Crusable, 12 November 2013 - 05:14 AM.

"C spilled his beer all over C++'s shirt. Outraged, C++ shouted, "Good god, man! Have you no class?"

"Your mother is so fat that the recursive function that was used to calculate her mass created a stack overflow"

 

 


#4 N.I.B.   Members   -  Reputation: 1021

Like
0Likes
Like

Posted 12 November 2013 - 05:30 AM

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



#5 Crusable   Members   -  Reputation: 590

Like
0Likes
Like

Posted 12 November 2013 - 06:57 AM

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


"C spilled his beer all over C++'s shirt. Outraged, C++ shouted, "Good god, man! Have you no class?"

"Your mother is so fat that the recursive function that was used to calculate her mass created a stack overflow"

 

 


#6 N.I.B.   Members   -  Reputation: 1021

Like
0Likes
Like

Posted 12 November 2013 - 07:03 AM

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?



#7 cgrant   Members   -  Reputation: 398

Like
0Likes
Like

Posted 12 November 2013 - 07:41 AM

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.



#8 Álvaro   Crossbones+   -  Reputation: 10598

Like
4Likes
Like

Posted 12 November 2013 - 07:52 AM

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



#9 Bregma   Crossbones+   -  Reputation: 4357

Like
0Likes
Like

Posted 12 November 2013 - 08:15 AM

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

#10 Eck   Members   -  Reputation: 1222

Like
0Likes
Like

Posted 12 November 2013 - 08:31 AM

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



#11 NightCreature83   Crossbones+   -  Reputation: 2477

Like
4Likes
Like

Posted 12 November 2013 - 08:40 AM

In the future use vld2 as it will extend the VS2012 memory leak tracker with proper callstacks, this makes it a lot easier to track where your leaks are coming from.

 

Your leaks will look more like this:


---------- Block 1199 at 0x04BE1058: 136 bytes ----------
Call Stack:
d:\Foobar\FooLog.cpp (26): FooLog::getInstance
d:\Foobar\FooMain.cpp (75): FooMain::init
f:\dd\vctools\crt_bld\self_x86\crt\src\crtexe.c (578): __tmainCRTStartup
f:\dd\vctools\crt_bld\self_x86\crt\src\crtexe.c (403): WinMainCRTStartup
0x759A3677 (File and line number not available): BaseThreadInitThunk
0x770C9D42 (File and line number not available): RtlInitializeExceptionChain
0x770C9D15 (File and line number not available): RtlInitializeExceptionChain
Data:
9C 33 2D 6B 74 2A 2D 6B C8 11 BE 04 00 00 00 00 .3-kt*-k ........
00 00 00 00 70 14 BB 6C 70 14 BB 6C 00 00 00 00 ....p..l p..l....
00 00 00 00 68 14 BB 6C 68 14 BB 6C 00 00 00 00 ....h..l h..l....
00 00 00 00 6C 14 BB 6C 6C 14 BB 6C 20 12 BE 04 ....l..l l..l....
00 00 00 00 CD 00 CD CD 00 00 00 00 01 CD CD CD ........ ........
68 14 BB 6C 78 33 2D 6B 00 00 00 00 00 00 00 00 h..lx3-k ........
00 00 00 00 01 02 00 00 06 00 00 00 00 00 00 00 ........ ........
00 00 00 00 00 00 00 00 88 11 BE 04 5C 10 BE 04 ........ ....\...
00 00 00 00 20 CD CD CD ........ ........

The output lines are double click able to jump to the actual allocation, and instead of the define all you need to do is include vld.h somewhere, point to the lib and include dir and all is done, in release vld will not compile with your project so no need for markers around the header.

 


Edited by NightCreature83, 12 November 2013 - 08:43 AM.

Worked on titles: CMR:DiRT2, DiRT 3, DiRT: Showdown, GRID 2, Mad Max

#12 Álvaro   Crossbones+   -  Reputation: 10598

Like
1Likes
Like

Posted 12 November 2013 - 08:46 AM

Here's something else you can do: Reduce your program to the simplest possible program that still shows a leak. Just make a copy of your code and start taking out anything that doesn't have to do with the problem at hand. You'll get to a point where anything you remove will stop showing the problem. We'll call that a "minimal program".

 

In the process of finding that minimal program, you'll very often figure out the problem yourself. And if you don't, you can always post the program here (it shouldn't be more than 40 lines) and you'll get much better help.

 

This is a general procedure that works for many types of bugs.



#13 Crusable   Members   -  Reputation: 590

Like
1Likes
Like

Posted 12 November 2013 - 11:26 AM

Thank you everyone. I figured out the error was with my singleton, it wasn't deleting properly. Thanks for the help.


"C spilled his beer all over C++'s shirt. Outraged, C++ shouted, "Good god, man! Have you no class?"

"Your mother is so fat that the recursive function that was used to calculate her mass created a stack overflow"

 

 





Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS