Jump to content

  • Log In with Google      Sign In   
  • Create Account


Can't find memory leak


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
13 replies to this topic

#1 Infinity95   Members   -  Reputation: 272

Like
0Likes
Like

Posted 02 July 2013 - 06:16 AM

Hello guys,

i'm currently having issues finding a memory leak in my code. When i hold down DEL it creates new objects of the Entity class and after a set time deletes them again. The problem now is, that somewhere in the process there is a memory leak but i can't find where. Any suggestions on how to find it? If you want to have a look at my code click here. The relevant files should be Game.cpp and Entity.cpp


"Errare humanum est, sed in errare perseverare diabolicum."


Sponsor:

#2 Muzzy A   Members   -  Reputation: 635

Like
8Likes
Like

Posted 02 July 2013 - 06:29 AM

// Include these in main.cpp
#define _CRTDBG_MAP_ALLOC
#include <stdlib.h>
#include <crtdbg.h>
 
// Put this at the top of your winmain function
_CrtSetDbgFlag ( _CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF );
_CrtSetReportMode( _CRT_ERROR, _CRTDBG_MODE_DEBUG );
 
_CrtSetBreakAlloc( -1 ); // Important line
 
// Run the code and get your memory leaks, it will give you a number in brackets for the memory leak in the output window
 
// Change the -1 value on the important line to the number you were given and run again
 
// It will break when you allocate memory to that same exact object that was leaked before, this is very helpful for finding leaks.
 

 

 

If that doesn't help you I'll take a look at your code. Good Luck :)



#3 Infinity95   Members   -  Reputation: 272

Like
0Likes
Like

Posted 02 July 2013 - 06:35 AM

Thanks, i'll try looking with that after my PE classes. I think this will help :)


"Errare humanum est, sed in errare perseverare diabolicum."


#4 Orangeatang   Members   -  Reputation: 1463

Like
0Likes
Like

Posted 02 July 2013 - 08:09 AM


If that doesn't help you I'll take a look at your code. Good Luck

That is by far the most useful thing I've learned today :) Thanks!



#5 Infinity95   Members   -  Reputation: 272

Like
0Likes
Like

Posted 02 July 2013 - 10:24 AM

Hmm ok i looked for memory leaks and found a few. They're now fixed but the main leak is still there. I can't seem to figure out where it occurs. All i know is it has something to do with the Entity class. :S Any help there? (I updated the repository again.)


"Errare humanum est, sed in errare perseverare diabolicum."


#6 SiCrane   Moderators   -  Reputation: 9573

Like
3Likes
Like

Posted 02 July 2013 - 10:37 AM

Since that leak detection code sounds like it compiled then you're probably using MSVC. In that case you can try using Visual Leak Detector which will give you the full call stack for any leaked allocations.

#7 alnite   Crossbones+   -  Reputation: 2086

Like
0Likes
Like

Posted 02 July 2013 - 01:31 PM

I was looking at your Graphics.cpp file, you don't seem to ever delete the texture pointers.

TEXTURE tex;
texID = texuid;
texFile = texFile;
tex.pTexture = pTexture;

this->textures.push_back(tex);

You never iterated through the textures vector and release them.



#8 Infinity95   Members   -  Reputation: 272

Like
0Likes
Like

Posted 02 July 2013 - 01:59 PM

Ok, thanks this tool helps. But im a bit confused at the moment. One memory leak seems to be in this line but i have absolutely no clue what should be wrong??

this->entityData.name = (char*)sqlite3_column_text(sqlQuery, 1);

EDIT: The texture pointers don't cause memory leaks. I can't tell you why exactly at the moment but it doesn't show up. Even if, it wouldn't be my main problem. The problem is that with every new Entity the memory leak gains size.

 

EDIT2: Just to be sure i added a loop and released the textures. Still no change.

Just so you can see how big this really is:

Holding DEL for about 15 seconds produces this:

 

Visual Leak Detector detected 2815 memory leaks (131372 bytes).
Largest number used: 171866 bytes.
Total allocations: 10732380 bytes.

 

EDIT3: At the moment it seems like the 2 strings in my Entity class aren't properly deallocated. Though i have no idea why, because i have a destructor in my class and it should be called when using delete, shouldn't it?


Edited by Infinity95, 02 July 2013 - 02:36 PM.

"Errare humanum est, sed in errare perseverare diabolicum."


#9 Muzzy A   Members   -  Reputation: 635

Like
0Likes
Like

Posted 02 July 2013 - 03:18 PM

Ok, thanks this tool helps. But im a bit confused at the moment. One memory leak seems to be in this line but i have absolutely no clue what should be wrong??

this->entityData.name = (char*)sqlite3_column_text(sqlQuery, 1);

 

'sqlite3_column_text' might be allocating memory you have to delete because it's a string.

 

Does your player entity get leaks as well? Or do you have to many entities to know? if so then just remove all but the player and check him. It would make it easier work with 1 entity at a time. Step through everything line by line.

 

I don't see anything being deleted at all in your Entity destructor, unless you're doing that somewhere else.


Edited by Muzzy A, 02 July 2013 - 03:18 PM.


#10 Infinity95   Members   -  Reputation: 272

Like
0Likes
Like

Posted 02 July 2013 - 03:28 PM

Well i don't see how i should delete string that are allocated on the stack. Is that even possible? Also i only have one Entity at the moment and that is the player entity. This causes 4 leaks:

WARNING: Visual Leak Detector detected memory leaks!
---------- Block 121 at 0x008A4850: 8 bytes ----------
  Call Stack:
    c:\program files (x86)\microsoft visual studio 11.0\vc\include\xstring (671): 2dGame.exe!std::_String_alloc<0,std::_String_base_types<char,std::allocator<char> > >::_Alloc_proxy + 0xA bytes
    c:\program files (x86)\microsoft visual studio 11.0\vc\include\xstring (651): 2dGame.exe!std::_String_alloc<0,std::_String_base_types<char,std::allocator<char> > >::_String_alloc<0,std::_String_base_types<char,std::allocator<char> > >
    c:\program files (x86)\microsoft visual studio 11.0\vc\include\xstring (749): 2dGame.exe!std::basic_string<char,std::char_traits<char>,std::allocator<char> >::basic_string<char,std::char_traits<char>,std::allocator<char> > + 0x56 bytes
    0x00B6554D (File and line number not available): 2dGame.exe!EntityDesc::EntityDesc + 0x4D bytes
    c:\users\mark\documents\visual studio 2012\projects\2dgame\2dgame\entity.cpp (4): 2dGame.exe!Entity::Entity + 0x68 bytes
    c:\users\mark\documents\visual studio 2012\projects\2dgame\2dgame\game.cpp (266): 2dGame.exe!Game::LoadLevel + 0x32 bytes
    c:\users\mark\documents\visual studio 2012\projects\2dgame\2dgame\game.cpp (97): 2dGame.exe!Game::Start + 0x7 bytes
    c:\users\mark\documents\visual studio 2012\projects\2dgame\2dgame\main.cpp (33): 2dGame.exe!WinMain + 0x9 bytes
    f:\dd\vctools\crt_bld\self_x86\crt\src\crtexe.c (528): 2dGame.exe!__tmainCRTStartup + 0x15 bytes
    f:\dd\vctools\crt_bld\self_x86\crt\src\crtexe.c (377): 2dGame.exe!WinMainCRTStartup
    0x75C833AA (File and line number not available): kernel32.dll!BaseThreadInitThunk + 0x12 bytes
    0x77089EF2 (File and line number not available): ntdll.dll!RtlInitializeExceptionChain + 0x63 bytes
    0x77089EC5 (File and line number not available): ntdll.dll!RtlInitializeExceptionChain + 0x36 bytes
  Data:
    A8 8A 8A 00    00 00 00 00                                   ........ ........


---------- Block 122 at 0x008A4930: 8 bytes ----------
  Call Stack:
    c:\program files (x86)\microsoft visual studio 11.0\vc\include\xstring (671): 2dGame.exe!std::_String_alloc<0,std::_String_base_types<char,std::allocator<char> > >::_Alloc_proxy + 0xA bytes
    c:\program files (x86)\microsoft visual studio 11.0\vc\include\xstring (651): 2dGame.exe!std::_String_alloc<0,std::_String_base_types<char,std::allocator<char> > >::_String_alloc<0,std::_String_base_types<char,std::allocator<char> > >
    c:\program files (x86)\microsoft visual studio 11.0\vc\include\xstring (749): 2dGame.exe!std::basic_string<char,std::char_traits<char>,std::allocator<char> >::basic_string<char,std::char_traits<char>,std::allocator<char> > + 0x56 bytes
    0x00B65562 (File and line number not available): 2dGame.exe!EntityDesc::EntityDesc + 0x62 bytes
    c:\users\mark\documents\visual studio 2012\projects\2dgame\2dgame\entity.cpp (4): 2dGame.exe!Entity::Entity + 0x68 bytes
    c:\users\mark\documents\visual studio 2012\projects\2dgame\2dgame\game.cpp (266): 2dGame.exe!Game::LoadLevel + 0x32 bytes
    c:\users\mark\documents\visual studio 2012\projects\2dgame\2dgame\game.cpp (97): 2dGame.exe!Game::Start + 0x7 bytes
    c:\users\mark\documents\visual studio 2012\projects\2dgame\2dgame\main.cpp (33): 2dGame.exe!WinMain + 0x9 bytes
    f:\dd\vctools\crt_bld\self_x86\crt\src\crtexe.c (528): 2dGame.exe!__tmainCRTStartup + 0x15 bytes
    f:\dd\vctools\crt_bld\self_x86\crt\src\crtexe.c (377): 2dGame.exe!WinMainCRTStartup
    0x75C833AA (File and line number not available): kernel32.dll!BaseThreadInitThunk + 0x12 bytes
    0x77089EF2 (File and line number not available): ntdll.dll!RtlInitializeExceptionChain + 0x63 bytes
    0x77089EC5 (File and line number not available): ntdll.dll!RtlInitializeExceptionChain + 0x36 bytes
  Data:
    C4 8A 8A 00    00 00 00 00                                   ........ ........


---------- Block 141 at 0x008CB168: 16 bytes ----------
  Call Stack:
    c:\program files (x86)\microsoft visual studio 11.0\vc\include\xstring (2196): 2dGame.exe!std::basic_string<char,std::char_traits<char>,std::allocator<char> >::_Copy + 0x27 bytes
    c:\program files (x86)\microsoft visual studio 11.0\vc\include\xstring (2227): 2dGame.exe!std::basic_string<char,std::char_traits<char>,std::allocator<char> >::_Grow + 0x13 bytes
    c:\program files (x86)\microsoft visual studio 11.0\vc\include\xstring (1133): 2dGame.exe!std::basic_string<char,std::char_traits<char>,std::allocator<char> >::assign + 0xE bytes
    c:\program files (x86)\microsoft visual studio 11.0\vc\include\xstring (1145): 2dGame.exe!std::basic_string<char,std::char_traits<char>,std::allocator<char> >::assign
    c:\program files (x86)\microsoft visual studio 11.0\vc\include\xstring (990): 2dGame.exe!std::basic_string<char,std::char_traits<char>,std::allocator<char> >::operator=
    c:\users\mark\documents\visual studio 2012\projects\2dgame\2dgame\entity.cpp (40): 2dGame.exe!Entity::Entity
    c:\users\mark\documents\visual studio 2012\projects\2dgame\2dgame\game.cpp (266): 2dGame.exe!Game::LoadLevel + 0x32 bytes
    c:\users\mark\documents\visual studio 2012\projects\2dgame\2dgame\game.cpp (97): 2dGame.exe!Game::Start + 0x7 bytes
    c:\users\mark\documents\visual studio 2012\projects\2dgame\2dgame\main.cpp (33): 2dGame.exe!WinMain + 0x9 bytes
    f:\dd\vctools\crt_bld\self_x86\crt\src\crtexe.c (528): 2dGame.exe!__tmainCRTStartup + 0x15 bytes
    f:\dd\vctools\crt_bld\self_x86\crt\src\crtexe.c (377): 2dGame.exe!WinMainCRTStartup
    0x75C833AA (File and line number not available): kernel32.dll!BaseThreadInitThunk + 0x12 bytes
    0x77089EF2 (File and line number not available): ntdll.dll!RtlInitializeExceptionChain + 0x63 bytes
    0x77089EC5 (File and line number not available): ntdll.dll!RtlInitializeExceptionChain + 0x36 bytes
  Data:
    CD CD CD CD    CD CD CD CD    CD CD CD CD    CD CD CD CD     ........ ........


---------- Block 148 at 0x008CB1B8: 16 bytes ----------
  Call Stack:
    c:\program files (x86)\microsoft visual studio 11.0\vc\include\xstring (2196): 2dGame.exe!std::basic_string<char,std::char_traits<char>,std::allocator<char> >::_Copy + 0x27 bytes
    c:\program files (x86)\microsoft visual studio 11.0\vc\include\xstring (2227): 2dGame.exe!std::basic_string<char,std::char_traits<char>,std::allocator<char> >::_Grow + 0x13 bytes
    c:\program files (x86)\microsoft visual studio 11.0\vc\include\xstring (1133): 2dGame.exe!std::basic_string<char,std::char_traits<char>,std::allocator<char> >::assign + 0xE bytes
    c:\program files (x86)\microsoft visual studio 11.0\vc\include\xstring (1145): 2dGame.exe!std::basic_string<char,std::char_traits<char>,std::allocator<char> >::assign
    c:\program files (x86)\microsoft visual studio 11.0\vc\include\xstring (990): 2dGame.exe!std::basic_string<char,std::char_traits<char>,std::allocator<char> >::operator=
    c:\users\mark\documents\visual studio 2012\projects\2dgame\2dgame\entity.cpp (69): 2dGame.exe!Entity::Entity
    c:\users\mark\documents\visual studio 2012\projects\2dgame\2dgame\game.cpp (266): 2dGame.exe!Game::LoadLevel + 0x32 bytes
    c:\users\mark\documents\visual studio 2012\projects\2dgame\2dgame\game.cpp (97): 2dGame.exe!Game::Start + 0x7 bytes
    c:\users\mark\documents\visual studio 2012\projects\2dgame\2dgame\main.cpp (33): 2dGame.exe!WinMain + 0x9 bytes
    f:\dd\vctools\crt_bld\self_x86\crt\src\crtexe.c (528): 2dGame.exe!__tmainCRTStartup + 0x15 bytes
    f:\dd\vctools\crt_bld\self_x86\crt\src\crtexe.c (377): 2dGame.exe!WinMainCRTStartup
    0x75C833AA (File and line number not available): kernel32.dll!BaseThreadInitThunk + 0x12 bytes
    0x77089EF2 (File and line number not available): ntdll.dll!RtlInitializeExceptionChain + 0x63 bytes
    0x77089EC5 (File and line number not available): ntdll.dll!RtlInitializeExceptionChain + 0x36 bytes
  Data:
    CD CD CD CD    CD CD CD CD    CD CD CD CD    CD CD CD CD     ........ ........


Visual Leak Detector detected 4 memory leaks (192 bytes).
Largest number used: 3418 bytes.
Total allocations: 23358 bytes.
Visual Leak Detector is now exiting.
The program '[7376] 2dGame.exe' has exited with code 0 (0x0).

However i can't see why there should be a leak at those points.


"Errare humanum est, sed in errare perseverare diabolicum."


#11 Bregma   Crossbones+   -  Reputation: 5053

Like
0Likes
Like

Posted 02 July 2013 - 04:10 PM

Every Game::EntitiesPtrs.pop_back(); call leaks an entity if you didn't delete first.  Maybe not oodles, but ClearEntities() certainly leaks.


Edited by Bregma, 02 July 2013 - 04:12 PM.

Stephen M. Webb
Professional Free Software Developer

#12 Infinity95   Members   -  Reputation: 272

Like
0Likes
Like

Posted 02 July 2013 - 04:25 PM

Clearentities only pops if the pointer is a Null pointer, meaning the object was already removed with delete! ClearEntities() shouldn't leak anything. I always delete the Entities before popping. Also i doubt that the Entity class only takes 16 bytes of memory for each instance.


Edited by Infinity95, 02 July 2013 - 04:27 PM.

"Errare humanum est, sed in errare perseverare diabolicum."


#13 Pink Horror   Members   -  Reputation: 1155

Like
3Likes
Like

Posted 02 July 2013 - 09:42 PM

You're zeroing over constructed STL objects:
SecureZeroMemory(&this->entityData, sizeof(this->entityData));



#14 Infinity95   Members   -  Reputation: 272

Like
0Likes
Like

Posted 03 July 2013 - 03:50 AM

You're zeroing over constructed STL objects:
SecureZeroMemory(&this->entityData, sizeof(this->entityData));

Thank you! That was the problem. I just manually set everything to default values now and the memory leak is gone. Thanks a lot for the help all!


"Errare humanum est, sed in errare perseverare diabolicum."





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