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

Robert Osfield robert.osfield at gmail.com
Thu Jun 25 09:54:03 PDT 2015


Hi Christian,

I can't spot a problem with a quick read through, copy and paste code in
isolation isn't sufficient to spot what might be wrong.

As a general note the OSG ref counting is extremely robust, hammered by
many thousands of developers in over a decade of it's existence, it's
unlikely that that this will be the source of what or what may not be a
leak.

While ref counting is robust when used correctly there are ways to cause
problems - creating circular references in the scene graph is one example,
another is retaining a ref_ptr<> to objects globally but not keeping track
of these, another is code where ref/unref() is called manually without them
being properly balanced.

If you want assistance from the community perhaps the best way would be to
create a small example that illustrates the problem.  It might be that you
spot the error in your code just doing this, but if then a nice succinct
example that others can review and test first hand will provide the best
means of getting to the bottom of the problem.

Robert.


On 25 June 2015 at 17:42, Christian Kunz <christian.kunz83 at gmail.com> wrote:

> 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
>
>
>
>
>
> _______________________________________________
> osg-users mailing list
> osg-users at lists.openscenegraph.org
> http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.openscenegraph.org/pipermail/osg-users-openscenegraph.org/attachments/20150625/c413c5c2/attachment-0003.htm>


More information about the osg-users mailing list