Jump to content
  • Advertisement
Sign in to follow this  
tanaris44

Memory "Leak" in Direct3D Game

This topic is 3047 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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;
	}
}

Share this post


Link to post
Share on other sites
Advertisement
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!