[osg-users] Memory leak when using a pixel buffer to generate thumbnails

OpenSceneGraph Users osg-users at lists.openscenegraph.org
Tue Jun 23 07:33:24 PDT 2020

Hi Robert,

I am using code extracted from the osgscreencapture example to generate 
small thumbnails of loaded shapes.
It all work is very nicely except that it is leaking memory on each usage.  
I have commented out our code and based on that and a few other tests I am 
fairly convinced that the leak is the pixel buffer (GraphicsContext) itself.

createPixelBuffer(int aSize, int aNumSamples)
    osg::ref_ptr<osg::GraphicsContext::Traits> traits = new 
    traits->x                                         = 0;
    traits->y                                         = 0;
    traits->width                                     = aSize;
    traits->height                                    = aSize;
    traits->red                                       = 8;
    traits->green                                     = 8;
    traits->blue                                      = 8;
    traits->alpha                                     = 8;
    traits->windowDecoration                          = false;
    traits->pbuffer                                   = true;
    traits->doubleBuffer                              = true;
    traits->sharedContext                             = 0;

    if (aNumSamples != 0)
        traits->sampleBuffers = 1;
        traits->samples       = aNumSamples;

    return osg::GraphicsContext::createGraphicsContext(traits.get());

generateThumbnail(const std::string & aFilePath,
                  const std::string & aThumbnailPath,
                  const std::string & aResourcesPath,
                  int                 aSize,
                  bool                aTopDownView)
    auto pixelBuffer = createPixelBuffer(aSize, 8);
    if (!pixelBuffer)
        pixelBuffer = createPixelBuffer(aSize, 0);

//    const auto shape = loadShapeOrError(aFilePath);

    osg::ref_ptr<osgViewer::Viewer> viewer = new osgViewer::Viewer;
    auto              camera = viewer->getCamera();

    camera->setViewport(new osg::Viewport(0, 0, aSize, aSize));

//    setupShapeViewer(viewer, shape.get(), aResourcesPath, aTopDownView);

//    osg::ref_ptr<ImageGrabber> grabber = new ImageGrabber(aThumbnailPath);
//    viewer.getCamera()->setPostDrawCallback(grabber.get());


    return true;

If I call the above code repeatedly then it leaks and I cannot understand 
why since everything is in osg:ref_ptr<>. 
The Viewer was a concrete instance originally but that makes no difference.
I have also tried using a slave camera as in the original example.
I have a nasty workaround of extracting a shared pixel buffer and re-using 
it - but ideally this code would be called using std::async, to have 
multiple thumbnails generated in parallel, so that is going to be tricky.

Kinda hoping you can point to my stupid mistake in how I am using osg ;)



You received this message because you are subscribed to the Google Groups "OpenSceneGraph Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to osg-users+unsubscribe at googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/osg-users/acdd2682-5b66-4064-aed1-66d2e5c9e3d5o%40googlegroups.com.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.openscenegraph.org/pipermail/osg-users-openscenegraph.org/attachments/20200623/bd9d58d7/attachment.html>

More information about the osg-users mailing list