Sign in to follow this  

Memory Leak During Program Exit...

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

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?

Share this post


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

Share this post


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

Share this post


Link to post
Share on other sites
Did you notice that you're calling d3d9Sprite->OnLostDevice() and not Release()?

Share this post


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

Share this post


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

Share this post


Link to post
Share on other sites
Yea, and I release all the textures that they use before exiting, so not sure what it doesn't like.

Share this post


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

Share this post


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

Share this post


Link to post
Share on other sites
Nevermind, I didn't need to call new D3DXVECTOR3 I can get by with calling &D3DXVECTOR3 and passing it the parameters instead.

Share this post


Link to post
Share on other sites
First, as suggested, call d3d9Sprite->Release().

What do the constructor and destructor of DXTexture do?

EDIT: if resolved, ignore.

Share this post


Link to post
Share on other sites
Nah, it was Code Guard from Borland. Thanks for the help Steve. You've been a big contributor to the success of this project I am working on thus far.

I'd like you to know that after getting it up and running in Visual Studio I was actually able to get it running in Borland Builder 6. Definately wasn't anywhere near as easy to do, but I have it up and running now and am starting to get a better understanding of how all this works.

If anyone needs any help on here and comes across this mention, I can give help with them setting up and getting it running in Borland Builder 6 now, so maybe I can help contribute to someone else learning it.

Share this post


Link to post
Share on other sites
I use and always worked this way:

#ifndef SAFE_DELETE
#define SAFE_DELETE(p) { if(p) { delete (p); (p)=NULL; } }
#endif
#ifndef SAFE_DELETE_ARRAY
#define SAFE_DELETE_ARRAY(p) { if(p) { delete[] (p); (p)=NULL; } }
#endif
#ifndef SAFE_RELEASE
#define SAFE_RELEASE(p) { if(p) { (p)->Release(); (p)=NULL; } }
#endif

Share this post


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