# Strange Memory leak involving texture allocation

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

## 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-&gt;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-&gt;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 on other sites
Quote:
 Original post by sipicklesI 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 on other sites
I apologise, I meant to put SAFE_RELEASE (m_normalMap ).Doh!

##### Share on other sites
Quote:
 Original post by sipicklesI 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.

• 23
• 10
• 19
• 15
• 14