Sign in to follow this  
Waaayoff

My game is leaking 14.5 Mb of memory per app??

Recommended Posts

eh my semi-complete tetris 3D game is leaking memory. When i run in debug mode, i get this: (The first line is repeated with various values a LOT) Direct3D9: (ERROR) :Memory Address: 03ffa604 lAllocID=515 dwSize=0000003c, ReturnAddr=722099aa (pid=00001cfc) Direct3D9: (ERROR) :Total Memory Unfreed From Current Process = 1459823 bytes Direct3D9: :====> EXIT: DLLMAIN(7220a170): Process Detach 00001cfc Now what am i supposed to do with this information?? Also, just a quick question.. In my game, i allocated some memory like this: OBJECT* pBlock = new OBJECT[NumBricks]; Now this pointer (pBlock) is passed to maybe 10 functions every frame (some every x milliseconds) When i'm done with a block i deallocate this memory in the last function like this: delete [] pBlock; And do this process all over again for every Tetris Block. So basically i allocate memory for every block and deallocate it when the block is no longer controlled by the player. My question is, am i doing it right? when i pass the pointer to a function, does it create another pointer that must be deallocated? Or do i only have to deallocate the pointer once, wherever i want to? Thanks :)

Share this post


Link to post
Share on other sites
When you pass a pointer to a function it does not perform any additional copying of the memory it points to. You do not have to call delete any additional times for passing a pointer to a function. If you call delete on a pointer more than once (without changing what it is pointing to) you will usually encounter a run time error.

The rule is for every new there should be a delete. You have described that you are doing this correctly with the blocks. Look for other places where you may be using the "new" keyword and make sure you are also deleting the objects appropriately. *Edit: You may also want to make sure you are not manhandling some functions which result in resource allocations in Direct3D without releasing them. I am not very familiar with D3D, always been more of an OpenGL guy myself, so I can't really tell you what to watch out for.

You may want to google RAII and attempt to follow that idea as it simplifies memory management, code with exceptions, and program control flow.

Tools do exist which offer line numbers of the memory leaks. Search "C++ Memory Leak Detection" I believe I've used Visual Leak Detector in the past, but I'm not sure if it is still being maintained. There may be better solutions.

Without seeing your code it is actually impossible for me to give you specific details as to what is leaking memory.

Share this post


Link to post
Share on other sites
Don't know what your code looks like, but (in addition to what M2tM posted) you might want to first check your assumption that new OBJECT[] is involved.

Comment out any call that uses your OBJECT pointer to end up with something like:

OBJECT* pBlock = new OBJECT[NumBricks];
// do something with pBlock - comment out
// do something else with pBlock - comment out
delete pBlock;

The intent is to see if this fixes the problem. If not, look elsewhere for new's as mentioned by M2tM. Also look for D3DXCreate...() calls and ensure everything you create is released.

With regard to D3DXCreate..() calls, it's a good habit to get into to initialize all your variables to NULL at the beginning of the app. Whenever you create something, precede the create call with

if( pTexture ) { pTexture->Release(); pTexture=NULL; }
D3DXLoadTextureFromFile(..., &pTexture);

Before your app closes, go through every object and:

for every D3D object:
if( d3dObject ) d3dObject->Release();


Do you release all your device objects (textures, etc.) and the D3D object and the D3DX device before the end of the app?

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this