Game Developer




Memory Leaks in C++

Category : Win8 Dev · by Jul 8th, 2012

Ahh, that’s a never-ended story. You can’t fight them all, but you could try eliminate most of them.

Once while debugging my Windows 8 C++ DirectX game I opened Windows Task Manager to check memory consumption. It is the worst tool for memory leaks checking, but if your program takes 150 MB additional memory in five minutes, something is wrong.

I am using extensively vector class, so I supposed the problem, is there. Not quite right, but very close.

There is my Code for adding new string in my string manager:


GameString* string = new GameString(text, position, color, GameStringState::OneTime, SpriteFont);
 string->Initialize(m_d2dContext.Get(), m_dwriteFactory.Get(), textRect);

m_stringList.push_back(*string); 

As you could see, I am creating string in heap first and then pass it to vector. And because it makes shallow copy, the old copy is not deleted. All I need to is

 delete string; 

after

 m_stringList.push_back(*string); 

I am doing the same job twice, it is not efficient, but it will do for now.

SHARE :

Leave a Reply

Your email address will not be published.