Memory Leak During Program Exit...

Started by
11 comments, last by VitaliBR 13 years, 6 months ago
I have my scenes being rendered correctly; however, it appears that its not freeing up the memory during exit because I keep getting a warning on program exit about it.

What all do I have to do to free up the memory that I was using during my program?

I went through and called release for all my textures, wiped out my texture list,
and called:
//-------------------	if ( navTexture != NULL )        navTexture->Release();	//-------------------    if ( d3d9VertexBuffer != NULL )        d3d9VertexBuffer->Release();    //-------------------    if ( d3d9Sprite != NULL)        d3d9Sprite->OnLostDevice();	//-------------------	if (d3d9Device != NULL)		d3d9Device->Release();	//-------------------	if (d3d9 != NULL)		d3d9->Release();	//-------------------


But it keeps giving me a warning about my sprites that I created during a Render call. Each time my position changes I fetch the necessary tiles I need, create a surface if the surface doesn't exist, then call sprite->draw for each of these textures. However, it doesn't seem to be freeing up all the memory used by the sprites that were drawn with that. Am I missing something?
Advertisement
Quote:Original post by nlraley
But it keeps giving me a warning about my sprites that I created during a Render call.

Can we see the code where you do this? As an aside, you shouldn't be creating anything at render time if you can help it.
Quote:Am I missing something?

If you're still getting memory leaks, then yes.

If you get any pointers from your surface object (like GetSurfaceLevel), it's likely those pointers need to be released also.

Please don't PM me with questions. Post them in the forums for everyone's benefit, and I can embarrass myself publicly.

You don't forget how to play when you grow old; you grow old when you forget how to play.

Did you notice that you're calling d3d9Sprite->OnLostDevice() and not Release()?
Release is not a method of Sprite. The reference for Sprite says that OnLostDevice performs the following:
Quote:Use this method to release all references to video memory resources and delete all stateblocks. This method should be called whenever a device is lost or before resetting a device.


Here is my code during the Render call:
void _Direct3D::Render(){	//-------------------	D3DCOLOR vertexColour = 0xFFFFFFFF;	//-------------------	d3d9Device->Clear(		0,								//  Number of Rectangles in Array - Set to 0 since Rects is NULL		NULL,							//  Pointer to an Array of D3DRect to Clear - NULL indicates entire viewport		D3DCLEAR_TARGET,				//  Surface to Clear		D3DCOLOR_XRGB( 0, 0, 0 ),		//  Clear to this Color		1.0f,							//  Depth Buffer to Clear to		0								//  Stencil Buffer to Clear to	);	//-------------------	if ( SUCCEEDED( hr = d3d9Device->BeginScene() ) )	{		//-------------------		//  Scene Rendering takes place Here		//-------------------		// Draw the triangles in the vertex buffer. This is broken into a few        // steps. We are passing the vertices down a "stream", so first we need        // to specify the source of that stream, which is our vertex buffer. Then        // we need to let D3D know what vertex shader to use. Full, custom vertex        // shaders are an advanced topic, but in most cases the vertex shader is        // just the FVF, so that D3D knows what type of vertices we are dealing        // with. Finally, we call DrawPrimitive() which does the actual rendering        // of our geometry (in this case, just one triangle).        //-------------------  Begin Sprite Drawing		d3d9Sprite->Begin(D3DXSPRITE_ALPHABLEND );        //-------------------  Draw the Map Tiles        DXTexture dxt = DXTexture(this);        dxt.DrawSprites();        //-------------------  Draw the Navigation Control        hr = d3d9Sprite->Draw(            navTexture,            NULL,            new D3DXVECTOR3(0.0f, 0.0f, 0.0f),            new D3DXVECTOR3(0.0f, 0.0f, 0.0f),            D3DCOLOR_ARGB(255, 255, 255, 255)        );        if (FAILED(hr))            return;        //-------------------  End Sprite Drawing		d3d9Sprite->End();		//-------------------  End the Scene		d3d9Device->EndScene();	}	//-------------------  Present the Backbuffer contents to the display	d3d9Device->Present( NULL, NULL, NULL, NULL);	//-------------------}//-------------------


And my function that is called for the dxt.DrawSprites(); is as follows:
HRESULT DXTexture::DrawSprites(){    //-------------------    std::list<LOADEDTEXTURE*>::iterator it;    //-------------------  Go through loaded texture list    for (it = loadedTextures.begin(); it != loadedTextures.end (); it++)    {        hr = DXDirect3D->d3d9Sprite->Draw(            (*it)->texture,            NULL,            new D3DXVECTOR3(0.0f, 0.0f, 0.0f),            new D3DXVECTOR3((*it)->xpos, (*it)->ypos, 0.0f),            D3DCOLOR_ARGB(255, 255, 255, 255)        );        if (FAILED(hr))            return hr;    }    //-------------------    return hr;    //-------------------}
Quote:Original post by nlraley
Release is not a method of Sprite.
No, but it's a method of IUnknown, which ID3DXSprite inherits from - and that code doesn't allocate any sprites, it just draws them.
Yea, and I release all the textures that they use before exiting, so not sure what it doesn't like.
Ah, its complaining about the:
new D3DXVECTOR3(0.0f, 0.0f, 0.0f),new D3DXVECTOR3((*it)->xpos, (*it)->ypos, 0.0f),


Part in my DrawSprites. How can I clear those out of the memory?
Oh, sorry - I thought you were talking about D3D leaks (From the debug runtimes).

You should change your Draw() call to:
hr = d3d9Sprite->Draw(            navTexture,            NULL,            NULL,            NULL,            D3DCOLOR_ARGB(255, 255, 255, 255)        );


EDIT: I didn't see you have two positions in there... For the second one, do:
D3DXVECTOR3 vPos((*it)->xpos, (*it)->ypos, 0.0f);hr = d3d9Sprite->Draw(            navTexture,            NULL,            NULL,            &vPos,            D3DCOLOR_ARGB(255, 255, 255, 255)        );
Nevermind, I didn't need to call new D3DXVECTOR3 I can get by with calling &D3DXVECTOR3 and passing it the parameters instead.

This topic is closed to new replies.

Advertisement