Page 1 of 1

[fixed]Terrain:D3D Memory Leak Problem (12.TerrainRendering)

Posted: Wed Feb 18, 2009 2:45 pm
by Ti-R
Hi,

I got a problem of memory leak on the 1.5 svn branch with "12.TerrainRendering_vc8".
If I activate "Debug D3D9 Runtime", and I run example 12 in debug mode from Visual C++ 2005, then I move the camera and quit, then I can see a bunch of memory leak at the end.
It seems the terrain generate thoses memory leak, and it doesn't allow me to reset my device inside my own program when I'm using this terrain.

Does someone got a clue about it ? or/and a solution ?

ps: It seems link to the Hardware Buffer.

Posted: Wed Feb 18, 2009 2:49 pm
by hybrid
Yes, you have to update to the latest SVN code, or wait for Irrlicht 1.5.1 or later, which will have this fixed.

Posted: Wed Feb 18, 2009 5:02 pm
by Ti-R
Just to help a bit.

If I comment thoses 2 lines to disable HardwareMapping

Code: Select all

CTerrainSceneNode::CTerrainSceneNode(...)
{
...
		RenderBuffer = new CDynamicMeshBuffer(video::EVT_2TCOORDS, video::EIT_16BIT);
//		RenderBuffer->setHardwareMappingHint(scene::EHM_STATIC, scene::EBT_VERTEX);
//		RenderBuffer->setHardwareMappingHint(scene::EHM_DYNAMIC, scene::EBT_INDEX);
...
}
The memory leak disappear.

Thanks hybrid for the reply

Posted: Thu Mar 26, 2009 4:31 pm
by Ti-R
Hi,

I'm trying to patch irrlicht 1.5 to fix this bug.

I find out irrlicht is creating a lot of IndexBuffer for the terrain LOD because the size of index is increasing following the camera position. It result having lot of IndexBuffer unrelease.

I just add

Code: Select all


bool CD3D9Driver::updateIndexHardwareBuffer(SHWBufferLink_d3d9 *HWBuffer)
{
//...
		/// (BEGIN) Patch (if indexBuffer exist, release it before to create a new one) ---- Ti-R ----
		if(HWBuffer->indexBuffer)
		{
			HWBuffer->indexBuffer->Release();
			HWBuffer->indexBuffer=0;
		}
		/// (END) Patch (if indexBuffer exist, release it before to create a new one) ---- Ti-R ----

		if(FAILED(pID3DDevice->CreateIndexBuffer( indexCount * indexSize, flags, indexType, D3DPOOL_DEFAULT, &HWBuffer->indexBuffer, NULL)))
//...
}
Same for VertexBuffer

Code: Select all

bool CD3D9Driver::updateVertexHardwareBuffer(SHWBufferLink_d3d9 *HWBuffer)
{
//...
		/// (BEGIN) Patch (if vertexBuffer exist, release it before to create a new one) ---- Ti-R ----
		if(HWBuffer->vertexBuffer)
		{
			HWBuffer->vertexBuffer->Release();
			HWBuffer->vertexBuffer=0;
		}
		/// (END) Patch (if vertexBuffer exist, release it before to create a new one) ---- Ti-R ----

		pID3DDevice->CreateVertexBuffer(vertexCount * vertexSize, flags, FVF, D3DPOOL_DEFAULT, &HWBuffer->vertexBuffer, NULL);
//...
}
Now D3D reset is working perfectly.

But in D3D Debug mode, I still see when I quit my program some memoryleak (If I comment both setHardwareMappingHint, like say before, they go away...! )

Any clue to correct this fix, did I miss something to free correctly buffer ?

BTW, I check that SetStreamSource(0, 0, 0, 0) & SetIndices(0) are always call after been set.

Posted: Thu Mar 26, 2009 5:07 pm
by Ti-R
I find the last leak. (The fix I made up is still valid)

Need also to remove the HardwareBuffer by "Hand" inside the
CTerrainSceneNode.

Code: Select all

CTerrainSceneNode::~CTerrainSceneNode()
{
//...

if (RenderBuffer)
{
/// (BEGIN) Patch (Remove HWBuffer by "Hand") ---- Ti-R ----
	SceneManager->getVideoDriver()->removeHardwareBuffer(RenderBuffer);
/// (END) Patch (Remove HWBuffer by "Hand") ---- Ti-R ----

	RenderBuffer->drop();}
//...
}

Could be very nice to have an automatic clean up when we drop a Hardware Buffer :wink: