[fixed] memory leak on grass

mickey

02-02-2009 09:41:06

Hi

Strange - when I make a call to:

gGrass->update();

When I terminate my application, I get a memory leak.

My grass init code looks like the ff:


//-------------------------------------- LOAD gGrass --------------------------------------
//Create and configure a new PagedGeometry instance for grass
gGrass = new PagedGeometry(mCamera, 100);
gGrass->addDetailLevel<GrassPage>(500);

//Create a GrassLoader object
GrassLoader *grassLoader = new GrassLoader(gGrass);
gGrass->setPageLoader(grassLoader); //Assign the "treeLoader" to be used to load geometry for the PagedGeometry instance

//Supply a height function to gGrassLoader so it can calculate gGrass Y values
//HeightFunction::initialize(mSceneMgr);
grassLoader->setHeightFunction(&getTerrainHeight);

//Add some grass to the scene with gGrassLoader::addLayer()
GrassLayer *l = grassLoader->addLayer("3D-Diggers/plant1sprite");

//Configure the grass layer properties (size, density, animation properties, fade settings, etc.)
l->setMinimumSize(2.5f, 2.5f);
l->setMaximumSize(6.0f, 6.0f);
l->setAnimationEnabled(true); //Enable animations
l->setSwayDistribution(10.0f); //Sway fairly unsynchronized
l->setSwayLength(0.5f); //Sway back and forth 0.5 units in length
l->setSwaySpeed(0.5f); //Sway 1/2 a cycle every second
l->setDensity(0.7f); //Relatively dense grass
l->setFadeTechnique(FADETECH_GROW); //Distant gGrass should slowly raise out of the ground when coming in range
l->setRenderTechnique(GRASSTECH_QUAD); //Draw gGrass as scattered quads

//This sets a color map to be used when determining the color of each grass quad. setMapBounds()
//is used to set the area which is affected by the color map. Here, "terrain_texture.jpg" is used
//to color the grass to match the terrain under it.
l->setColorMap("terrain_texture.jpg");
l->setMapBounds(TBounds(0, 0, 1500, 1500)); //(0,0)-(1500,1500) is the full boundaries of the terrain


And on my destructor:


if( gGrass )
{
delete gGrass->getPageLoader();
SAFE_DELETE(gGrass);
}


I don't get any memory leak if i don't make a call to gGrass->update(...).

Any idea?

Fish

03-02-2009 13:59:47

I save the pointer to grassLoader and then delete grassLoader followed by gGrass and do not get any memory leaks.

-Fish

mickey

04-02-2009 05:46:31

it didn't help. I saved a pointer like you said and deleted it (though im not sure why saving a copy would eventually help either, since getting the pointer from the gGrass is the same as saving a pointer and deleting the copy.

f( gGrass )
{
SAFE_DELETE(gGrassLoader);
SAFE_DELETE(gGrass);
}

Is you rinitialization more or less similar to mine?

mickey

08-02-2009 18:10:06

Anyone? I would try things...

jabberwocky

10-02-2009 22:09:00

The release notes for PagedGeometry v1.05 say this:
- (Bug Fix) Fixed memory leak in GrassLoader when multiple layers are used

If you're not using v1.05, you may want to upgrade.

Fish

22-02-2009 19:24:36

mickey is right, there is a memory leak in grassLoader.


Block 7458 at 0x01929FB8: 20 bytes @ grassloader.cpp (124): Forests::GrassLoader::loadPage
+ 20 or so others at the same line of code.


It appears that the page.userData is not being deleted somehow. So since the void* castings and conversions to and from std::vector<Mesh*> * made my head spin I converted them to something that is maybe a little more transparent (for me anyway :) ). This change also plugs the memory leak.


Index: include/GrassLoader.h
===================================================================
--- include/GrassLoader.h (revision 2654)
+++ include/GrassLoader.h (working copy)
@@ -172,7 +172,7 @@
/** INTERNAL FUNCTION - DO NOT USE */
void loadPage(PageInfo &page);
/** INTERNAL FUNCTION - DO NOT USE */
- void unloadPage(const PageInfo &page);
+ void unloadPage(PageInfo &page);
/** INTERNAL FUNCTION - DO NOT USE */
void frameUpdate();

Index: include/PagedGeometry.h
===================================================================
--- include/PagedGeometry.h (revision 2654)
+++ include/PagedGeometry.h (working copy)
@@ -103,6 +103,7 @@
#include <OgreCamera.h>
#include <OgreVector3.h>
#include <OgreTimer.h>
+#include <OgreMesh.h>

namespace Forests {

@@ -1121,6 +1122,8 @@
PageLoader::unloadPage() is called.
*/
void *userData;
+
+ std::vector<Ogre::Mesh*> meshList;
};

/**
Index: source/GrassLoader.cpp
===================================================================
--- source/GrassLoader.cpp (revision 2654)
+++ source/GrassLoader.cpp (working copy)
@@ -120,10 +120,6 @@
uint32 seed = (xSeed << 16) | zSeed;
srand(seed);

- //Keep a list of a generated meshes
- std::vector<Mesh*> *meshList = new std::vector<Mesh*>();
- page.userData = (void*)meshList;
-
//Generate meshes
std::list<GrassLayer*>::iterator it;
for (it = layerList.begin(); it != layerList.end(); ++it){
@@ -180,17 +176,17 @@
}
}

-void GrassLoader::unloadPage(const PageInfo &page)
+void GrassLoader::unloadPage(PageInfo &page)
{
//Unload meshes
- std::vector<Mesh*> *meshList = (std::vector<Mesh*>*)page.userData;
- std::vector<Mesh*>::iterator i;
- for (i = meshList->begin(); i != meshList->end(); ++i) {
+ std::vector<Mesh*>::const_iterator i;
+ std::vector<Mesh*>::const_iterator iter_end = page.meshList.end();
+ for (i = page.meshList.begin(); i != iter_end; ++i)
+ {
Mesh *mesh = *i;
MeshManager::getSingleton().remove(mesh->getName());
}
- meshList->clear();
- delete meshList;
+ page.meshList.clear();
}

Mesh *GrassLoader::generateGrass_QUAD(PageInfo &page, GrassLayer *layer, float *grassPositions, unsigned int grassCount)


edit: submitted to Ogre Addon Tracker.

-Fish

tdev

29-06-2009 11:38:07

the patch you provided does not compile, could you please update it?

Fish

30-06-2009 05:01:59

the patch you provided does not compile, could you please update it?

It compiles here just fine on MSVC8.0, MinGW, and XCode. Could you provide a little more information like what errors the compiler is throwing? Maybe it didn't diff out or merge in correctly...or both.

-Fish

Fish

30-06-2009 12:51:26

I just checked-out trunk and tried merging this patch. The patch didn't diff-out correctly. Posting a new diff to the tracker.

edit: Posted diff to tracker.

-Fish

tdev

30-06-2009 12:58:37

I just checked-out trunk and tried merging this patch. The patch didn't diff-out correctly. Posting a new diff to the tracker. thanks
nice idea about the bugtracker, but i have no permissions to moderate/close tasks. Will ask sinbad :)

Fish

30-06-2009 13:03:10

Oh okay. I'll post it here as well then.

Edit: Oh...and I put the <tab> characters in the way you like them. :wink:

[attachment=0]Memory Leak patch.zip[/attachment]

tdev

30-06-2009 22:45:18

Oh okay. I'll post it here as well then.

Edit: Oh...and I put the <tab> characters in the way you like them. :wink:

[attachment=0]Memory Leak patch.zip[/attachment]

indeed, your solution for the meshList seems to be much cleaner, thank you :)
merged.