Memory "Leak" in Direct3D Game

Started by
4 comments, last by themadme 14 years, 1 month ago
So I have made a nifty little 2D engine in Direct3D with sound, sprite classes, and some other basic stuff. Before I start going too much deeper, I want to make sure the foundation of the engine is solid, and I have discovered a strange issue. When my application is active, or when it is not active and the mouse pointer moves around over the application, my memory usage increases. The most noticeable problems are caused by the mouse pointer moving over the application (active or inactive), and holding down any key. These literally increase my game's memory usage 100KB/s and 38KB/s respectively (and it never drops back down). I am pulling these numbers from the task manager if it matters. The crazy part is, that if my game is NOT active, memory usage stays completely flat, even though ai is running, sprites are moving, music is playing etc. Oh, and I am not even listening for mouse input. I have posted my input.cpp file below, but am hoping someone has seen this before and can tell me what is happening here.

#include "stdafx.h"
#include "input.h"

LPDIRECTINPUT8 input = NULL;
LPDIRECTINPUTDEVICE8 keyboard = NULL;
BYTE keystate[256];

void initDInput(HWND hwnd)
{
	DirectInput8Create(
		GetModuleHandle(NULL), 
		DIRECTINPUT_VERSION,
		IID_IDirectInput8,
		(void**)&input,
		NULL);

	input->CreateDevice(
		GUID_SysKeyboard,
		&keyboard,
		NULL);

	keyboard->SetDataFormat(&c_dfDIKeyboard);

	keyboard->SetCooperativeLevel(hwnd, DISCL_NONEXCLUSIVE | DISCL_FOREGROUND);
		
}

void getInput()
{
	keyboard->Acquire();
	keyboard->GetDeviceState(256, (LPVOID)keystate);
}

int Key_Down(int key)
{
	return (keystate[key] & 0x80);
}

void releaseKeyboard()
{
	if (keyboard != NULL)
	{
		keyboard->Unacquire();
		keyboard->Release();
		keyboard=NULL;
	}
}

Advertisement
I think that your problem is hidden somewhere in / near the window message loop.
I agree from above,

Nothing is wrong what you have done.

The best thing to do, is to place breakpoints at certain places of your code. Have the task manager up, and step through each line of code whilst watching the memory of your application. Eventually in time, you will find out where in your code has a memory leak problem.
Thanks for the guidance! I ended up finding it in WinProc. Before I was using this as the first two lines of the function:
global_hwnd = hwnd;global_hdc = GetDC(hwnd);

I assume the problem was that this was continuously creating new copies of hwnd and the device context in memory every single time it ran.

I am now using this with no problems:
if (global_hdc == NULL){        global_hwnd = hwnd;	global_hdc = GetDC(hwnd);}

Thanks again. It's a wonderful feeling, watching the memory usage stay flat given how much is going on onscreen.
For future reference, there are better tools available. For instance Process Explorer or PerfMon will give you much more detailed statistics about your app's memory usage, including the number of GDI handles you have open.
Oh yeah i forgot about perfom but there is also an Memory(-Leak) and Exception Trace application at http://www.codeproject.com/KB/applications/leakfinder.aspx.

Its a very good application that can find anything that has not been deleted or dangling pointers

This topic is closed to new replies.

Advertisement