[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:



//****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.


int iCountPrimitives=_geometryLines->getNumPrimitiveSets();

_geometryLines->removePrimitiveSet(0, iCountPrimitives);


// set the colors as before, plus using the aobve
osg::Vec4Array* colors = new osg::Vec4Array;

// set the normal in the same way color.
osg::Vec3Array* normals = new osg::Vec3Array;

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


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...


Read this topic online here:

More information about the osg-users mailing list