Jump to content
  • Advertisement
Sign in to follow this  
sipickles

Strange Memory leak involving texture allocation

This topic is 4579 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

Hello, I have a function in my start up routine which checks whether a normal map has already been generated (ie the file is on the harddrive), and uses it if so, else it builds a new one and saves it. It works fine if I comment out the bit which checks for the existing file, and just rebuild it every time, but if I let the check run, I get unallocated memory. Heres the function:
HRESULT cTerrainManager::CreateNormalMap( void )
{
	HRESULT hr;
	WCHAR wszOutput[128];

	// see if file already exists
	FILE *pFile = fopen("terrain\\terra3\\normalMap.jpg", "rb");				// open the  file, read only, binary mode
	if ( pFile )
	{
		fclose(pFile);	
		D3DXCreateTextureFromFile( g_device, L"terrain\\terra3\\normalMap.jpg", &m_normalMap);

		StringCchPrintf( wszOutput, 128, L"cTerrainManager::CreateNormalMap - loaded from file\n" );
		g_log->Log( wszOutput );

		return S_OK;
	}

//	fclose(pFile);	


	// Fill the texture using D3DXFillTexture
	hr = D3DXFillTexture (m_normalMap, ColorFill, NULL);
	if (FAILED (hr))
	{
		MessageBox(NULL, L"cTerrainManager::CreateNormalMap", L"FAILED", MB_OK );
	    return hr;
	}

	D3DXSaveTextureToFile( L"terrain\\terra3\\normalMap.jpg", D3DXIFF_JPG, m_normalMap, NULL );
	StringCchPrintf( wszOutput, 128, L"cTerrainManager::CreateNormalMap - generated\n" );
	g_log->Log( wszOutput );

	return S_OK;
}

I am ofcourse using a SAFE_DELETE( m_normalMap ) in my destructor. Anyone spot what I am doing wrong? I am going round in circles :) Thank you! Simon

Share this post


Link to post
Share on other sites
Advertisement
Quote:
Original post by sipickles
I am ofcourse using a SAFE_DELETE( m_normalMap ) in my destructor.


Just an idea - what is your definition of SAFE_DELETE()? The macro's name implies that you are deleting the interface, whereas you should be releasing it.

For all D3D interfaces, you can use a macro like this:

#define SAFE_RELEASE( x ) if(x) { x->Release(); x = NULL; }

You might already be doing this - the name is just a bit confusing though.

Share this post


Link to post
Share on other sites
Quote:
Original post by sipickles
I apologise, I meant to put SAFE_RELEASE (m_normalMap ).Doh!

Did you mean you put the wrong one in your post here, or the wrong one in your code? If you are still experiencing problems, try checking the reference count that is returned after Release() is called. This might give some further insight.

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!