[fixed]meshes with static flag don't release memory on deletion

You discovered a bug in the engine, and you are sure that it is not a problem of your code? Just post it in here. Please read the bug posting guidelines first.
Post Reply
CuteAlien
Admin
Posts: 9646
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

[fixed]meshes with static flag don't release memory on deletion

Post by CuteAlien »

Problem reported by Thelorious in IRC was that addVolumeLightSceneNode increase memory usage when used inside a loop.

Here's a small test-program to reproduce it:

Code: Select all

 
// addVolumeLightSceneNode not releasing static mesh when removed.
 
#include <irrlicht.h>
#include <iostream>
 
using namespace irr;
 
#ifdef _MSC_VER
#pragma comment(lib, "Irrlicht.lib")
#endif
 
 
int main(int argc, char *argv[])
{
  IrrlichtDevice * Device = createDevice(video::EDT_OPENGL, core::dimension2d<u32>(640,480) );
  if (!Device)
    return false;
 
  scene::ISceneManager* smgr = Device->getSceneManager();
  gui::IGUIEnvironment* guiEnv = Device->getGUIEnvironment();
    video::IVideoDriver* videoDriver =  Device->getVideoDriver();
 
  while ( Device->run() )
  {
    if ( Device->isWindowActive() )
    {
// LeakHunter can be enabled in IrrCompileConfig.h
//    irr::core::array<const IReferenceCounted*> refBefore = LeakHunter::getReferenceCountedObjects();
//    std::cout << "before: " << refBefore.size() << "\n";
      irr::scene::IVolumeLightSceneNode* vlightnode = smgr->addVolumeLightSceneNode( 0, -1, 100, // Subdivisions on U axis
                                                        100, // Subdivisions on V axis
                                                        video::SColor(0, 255, 255, 255), // foot color
                                                          video::SColor(0, 0, 0, 0)); // tail color
 
      videoDriver->beginScene(true, true);
 
      smgr->drawAll();
      guiEnv->drawAll();
 
 
      videoDriver->endScene();
 
 
      vlightnode->remove();
      
//    irr::core::array<const IReferenceCounted*> refAfter = LeakHunter::getReferenceCountedObjects();
//    std::cout << "after: " << refAfter.size() << "\n";
    }
    Device->sleep( 1 );
  }
 
  Device->closeDevice();
  Device->drop();
  
//  irr::core::array<const IReferenceCounted*> refEnd = LeakHunter::getReferenceCountedObjects();
//  std::cout << "end: " << refEnd.size() << "\n";
 
  return 0;
}
 
I debuggedit and the problem is that CGeometryCreator::createVolumeLightMesh creates a mesh with EHM_STATIC as hardware mapping hint. Which is only released 20.000 frames later (by that time this goes into the gigabytes).

Now there is one workaround that would probably work - removing the meshbuffer from the videodriver when the node is removed. But for some reason none of the nodes with meshbuffer ever seem to do that for static meshes - so not sure right now if anything speaks against that. And certainly the better solution would be if meshbuffers remove themself on destruction - but I guess they miss the access to the videodriver.

Also - IVolumeLightSceneNode should maybe give access to the Mesh - probably could be derived from IMeshSceneNode. Then users could work around this at least.

I'm not deep enough into the hardwarebuffer stuff to feel comfortable to fix this myself without some feedback ...
IRC: #irrlicht on irc.libera.chat
Code snippet repository: https://github.com/mzeilfelder/irr-playground-micha
Free racer made with Irrlicht: http://www.irrgheist.com/hcraftsource.htm
Nadro
Posts: 1648
Joined: Sun Feb 19, 2006 9:08 am
Location: Warsaw, Poland

Re: meshes with static flag don't release memory on deletion

Post by Nadro »

In shader-pipeline branch I fixed this issue by grab/drop for hardware buffers (IMeshBuffer store pointer for IHardwareBuffer), I think that we might prepare similar solution for trunk, hovewer I prefer to stay with current solution in v1.9 (ogl-es will be merged with trunk in v1.9) and in next version just use shader-pipeline. Existing solution where hardware buffers need irr::core::map for searching and auto remove after 20.000 frames is really bad and slow for most advanced scenes.
Library helping with network requests, tasks management, logger etc in desktop and mobile apps: https://github.com/GrupaPracuj/hermes
hybrid
Admin
Posts: 14143
Joined: Wed Apr 19, 2006 9:20 pm
Location: Oldenburg(Oldb), Germany
Contact:

Re: meshes with static flag don't release memory on deletion

Post by hybrid »

Yeah, as you said. The deferred removal was made in order to not require additional driver access from the meshes, which are completely unaware of video driver stuff. I would have expected that lightvolumen is derived from IMeshscenenode and give access such that the hardware buffer can be explicitly removed. That's probably also the only viable way, making the scene nodes remove the hardware buffers themsevles seems to involved.
Of course, in case you really want to do such heavy usage stuff you can also always remove all hardware buffers from time to time.
CuteAlien
Admin
Posts: 9646
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Re: meshes with static flag don't release memory on deletion

Post by CuteAlien »

Nodes removing hardware buffers would feel like a workaround. Then again it would fix it without users running into that problem and only changes a few places. So wouldn't be hard to do this I guess.

Additionally deriving from IMeshSceneNode can still be ok - but I just took a look and IMeshSceneNode also has a setMesh function. I don't know how lightvolumes work, but I guess that might not make sense. So maybe better to just add a getMesh? (we do that already in so many classes that I wonder if we should have a hasGetMesh() function in ISceneNode ... OK, getting off-topic here ^_^)
IRC: #irrlicht on irc.libera.chat
Code snippet repository: https://github.com/mzeilfelder/irr-playground-micha
Free racer made with Irrlicht: http://www.irrgheist.com/hcraftsource.htm
CuteAlien
Admin
Posts: 9646
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Re: [fixed]meshes with static flag don't release memory on deletion

Post by CuteAlien »

OK, just noticed I still had this one on my todo. This got fixed recently in svn [r6599], so will work be in Irrlicht 1.9
Was actually rather trivial to fix - hardware buffers don't have to wait 20.000 frames when they keep the last reference to a meshbuffer. This won't release hw-buffers in all cases (if they link to a meshbuffer which is still in cache it won't help), but for this case the fix works and shouldn't have any downsides. And the case which caused me to run into this recently was also similar - dynamic meshes with static flag which were constantly re-created.

An alternative would have been to call removeAllHardwareBuffers() once in a while - just in case someone runs in situation where new fix isn't helping.
IRC: #irrlicht on irc.libera.chat
Code snippet repository: https://github.com/mzeilfelder/irr-playground-micha
Free racer made with Irrlicht: http://www.irrgheist.com/hcraftsource.htm
Post Reply