Hi all,

I'm currently testing my osg application and I sometimes (1/10000) get the
following warning:
"Warning: deleting still referenced object XXX of type 'XXX'
         the final reference count was 1, memory corruption possible."
Followed by a crash.

Looking at osg::Referenced::unref(), I wonder if this function is really
thread safe.
Indeed, nothing seems to prevent the _refCount variable to be modified
between the operator-- and the deletion of the object.
If a thread increments _refCount after the operator--, the delete is still
called and we get the warning.


I made a quick test to reproduce it:

#include <OpenThreads/Thread>
#include <osg/Image>

class MyThread :
    public OpenThreads::Thread {
public:
    MyThread(osg::Image* imagePtr) :
        _imagePtr(imagePtr) {
    }

    virtual ~MyThread() {
    }

    void run() {
        microSleep(50);
        _imageRefPtr = _imagePtr;
    }

    osg::Image* _imagePtr;
    osg::ref_ptr<osg::Image> _imageRefPtr;
};



int main(int argc, char** argv) {

    osg::Image* imagePtr = new osg::Image();
    MyThread t(imagePtr);

    osg::ref_ptr<osg::Image> imageRefPtr(imagePtr);
    t.startThread();
    imageRefPtr = 0;
    t.join();

    return 0;
}


I also needed to add a microsleep in osg::Referenced::unref() so that i
could reproduce the warning more easlily.

    if (needDelete)
    {
        OpenThreads::Thread::microSleep(1000);
        signalObserversAndDelete(true,true);
    }
    return newRef;


Thanks
Mikaƫl
_______________________________________________
osg-users mailing list
osg-users@lists.openscenegraph.org
http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org

Reply via email to