[osg-users] Memory leak with PrimitiveSets who stay alive

Christian Kunz christian.kunz83 at gmail.com
Thu Jun 25 09:42:38 PDT 2015


Hi everyone,

I have a big memory leak in my application.

Generally I have n Spheres I put in my scene. When I add a Sphere a Drawable is added to the scene, when I remove it, it disapears. So the ref_ptr system is working good here.

Now I want to connect all the Spheres with osg::PrimitiveSet::LINE_STRIP. That also works fine. The thing is, that the position of the Spheres are dynamically changing and it can be
that new ones appear, etc. So what I do is to just draw the LINE_STRIP every Frame new.



First I clear the old vertices array then iterate over my Spheres and add there positions:


Code:

 _verticesLines->resize(0);

//****for( ... iterate over Spheres and fill vertices array
_verticesLines->push_back(osg::Vec3(tmpTrackpoint.x, tmpTrackpoint.y, tmpTrackpoint.z+1));



So all the x, y, z positions of my spheres are now in the vertices array.


Code:
       

int iCountPrimitives=_geometryLines->getNumPrimitiveSets();

_geometryLines->removePrimitiveSet(0, iCountPrimitives);

 if(sizeTrackList>1)
{
_geometryLines->setVertexArray(_verticesLines);

// set the colors as before, plus using the aobve
osg::Vec4Array* colors = new osg::Vec4Array;
colors->push_back(osg::Vec4(0.7f,0.7f,0.7f,1.0f));
_geometryLines->setColorArray(colors);
_geometryLines->setColorBinding(osg::Geometry::BIND_OVERALL);

// set the normal in the same way color.
osg::Vec3Array* normals = new osg::Vec3Array;
normals->push_back(osg::Vec3(0.0f,-1.0f,0.0f));
_geometryLines->setNormalArray(normals);
_geometryLines->setNormalBinding(osg::Geometry::BIND_OVERALL);

osg::ref_ptr<osg::DrawArrays> drawArray = new osg::DrawArrays(osg::PrimitiveSet::LINE_STRIP,0,sizeTrackList);
_geometryLines->addPrimitiveSet(drawArray);

_geodeLines->addDrawable(_geometryLines);




Then I simply add a new DrawArrays instance to the geometry and before that I`m removing any other DrawArray from the geometry.
The problem with that is, that the count of LINE_STRIPS increases with every frame. so the last DrawArray is not deleted and stays in memory.
At this point I tried a lot and I don´t understand why the DrawArray objects are not destroyed.

osg::ref_ptr<osg::DrawArrays> drawArray = new osg::DrawArrays(osg::PrimitiveSet::LINE_STRIP,0,sizeTrackList); -> so drawArray is of type osg::ref_ptr,
when I remove it from the osg::Geometry "geometryLines" it is not referenced by any node and should be destroyed?

Am i´m seeing this wrong? What would be a proper solution to solve this memory leak? How can I destroy the last DrawArrays instance so that memory is not leaking?


... 

Thanks for your help...

Cheers,
Christian

------------------
Read this topic online here:
http://forum.openscenegraph.org/viewtopic.php?p=64195#64195








More information about the osg-users mailing list