Google

[fixed] memory leak on grass

JohnJ's system for forests and grass, see ogreaddons/forests

Moderators: OGRE Team, PagedGeometry Moderators

[fixed] memory leak on grass

Postby mickey » Mon Feb 02, 2009 10:41 am

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:

Code: Select all
//-------------------------------------- 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:

Code: Select all
      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?
mickey
 
Posts: 48
Joined: Mon Dec 15, 2008 2:47 pm

Re: memory leak on grass

Postby Fish » Tue Feb 03, 2009 2:59 pm

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

-Fish
User avatar
Fish
 
Posts: 262
Joined: Wed Aug 27, 2008 9:10 pm

Re: memory leak on grass

Postby mickey » Wed Feb 04, 2009 6:46 am

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
 
Posts: 48
Joined: Mon Dec 15, 2008 2:47 pm

Re: memory leak on grass

Postby mickey » Sun Feb 08, 2009 7:10 pm

Anyone? I would try things...
mickey
 
Posts: 48
Joined: Mon Dec 15, 2008 2:47 pm

Re: memory leak on grass

Postby jabberwocky » Tue Feb 10, 2009 11:09 pm

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.
jabberwocky
 
Posts: 5
Joined: Thu Nov 29, 2007 3:21 pm

Re: memory leak on grass

Postby Fish » Sun Feb 22, 2009 8:24 pm

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

Code: Select all
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.

Code: Select all
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
User avatar
Fish
 
Posts: 262
Joined: Wed Aug 27, 2008 9:10 pm

Re: memory leak on grass

Postby tdev » Mon Jun 29, 2009 11:38 am

the patch you provided does not compile, could you please update it?
User avatar
tdev
 
Posts: 106
Joined: Sun Dec 24, 2006 1:09 pm
Location: Germany

Re: memory leak on grass

Postby Fish » Tue Jun 30, 2009 5:01 am

tdev wrote: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
User avatar
Fish
 
Posts: 262
Joined: Wed Aug 27, 2008 9:10 pm

Re: memory leak on grass

Postby Fish » Tue Jun 30, 2009 12:51 pm

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
Last edited by Fish on Tue Jun 30, 2009 1:01 pm, edited 1 time in total.
User avatar
Fish
 
Posts: 262
Joined: Wed Aug 27, 2008 9:10 pm

Re: memory leak on grass

Postby tdev » Tue Jun 30, 2009 12:58 pm

Fish wrote: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 :)
User avatar
tdev
 
Posts: 106
Joined: Sun Dec 24, 2006 1:09 pm
Location: Germany

Re: memory leak on grass

Postby Fish » Tue Jun 30, 2009 1:03 pm

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:

Memory Leak patch.zip
(1.01 KiB) Downloaded 19 times
User avatar
Fish
 
Posts: 262
Joined: Wed Aug 27, 2008 9:10 pm

Re: memory leak on grass

Postby tdev » Tue Jun 30, 2009 10:45 pm

Fish wrote: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:

Memory Leak patch.zip

indeed, your solution for the meshList seems to be much cleaner, thank you :)
merged.
User avatar
tdev
 
Posts: 106
Joined: Sun Dec 24, 2006 1:09 pm
Location: Germany


Return to PagedGeometry

Who is online

Users browsing this forum: No registered users and 1 guest