From http://forum.worldwindcentral.com/sh...3331#post43331

Anyone attempt animating a ModelFeature and discover memory issues? I get a steady rapid memory leak when i periodically change the Latitude property of a ModelFeature.

But, the condition for the memory leak is apparently assignment to a different value. When i assign a constant value to Latitude periodically i don't get a memory leak, nor do I when I comment out the assignment line from the memory-leak-version of my test while leaving everything else the same.


Hi

Thanks to patmurris for providing the solution to my problem:

18:14:54 < patmurris> do you rebuild the mesh... and forgot to dispose() of old stuff ?

Sorry that I didn't realize you'd given me the answer at the time. The sequence of events that caused the memory leak:

(1) ModelFeature.Update(...) calls Initialize often because, since I'm changing the lat and lon often, the condition

(currentElevation != World.TerrainAccessor.GetElevationAt(Lat itude, Longitude))

evaluates to true often

(2) ModelFeature.Initialize(...) calls LoadColladaMesh(...)

(3) ModelFeature.LoadColladaMesh(...) doesn't call dispose on the existing meshes in the ModelFeature before creating new ones.

For now my solution is adding this:

Code:
this.meshElems = null;
to the ModelFeature constructor, and adding this:

Code:
if (meshElems != null)
{
	foreach (MeshElem me in meshElems)
	{
		if (me.mesh != null)
			me.mesh.Dispose();
		if (me.meshTextures != null)
			foreach (Texture t in me.meshTextures)
				if (t != null)
					t.Dispose();
	}
}
to ModelFeature.LoadColladaMesh(...) after
Code:
if (meshCount == 0)
	return;
My understanding is that this memory leak would affect animation and also changes in the vertical exaggeration, because changes in either would cause ModelFeature.Update(...) to call ModelFeature.Initialize(...). I also believe that ModelFeature.LoadDirectXMesh(...) should be updated similarly (I haven't tested that).