[osg-users] Potential bug in ref_ptr::assign

Hartwig Wiesmann hartwig.wiesmann at skywind.eu
Tue Oct 24 15:34:14 PDT 2017


Hi,

in ref_ptr you find the following code:


Code:

        template<class Other> void assign(const ref_ptr<Other>& rp)
        {
            if (_ptr==rp._ptr) return;
            T* tmp_ptr = _ptr;
            _ptr = rp._ptr;
            if (_ptr) _ptr->ref();
            // unref second to prevent any deletion of any object which might
            // be referenced by the other object. i.e rp is child of the
            // original _ptr.
            if (tmp_ptr) tmp_ptr->unref();
        }




I was puzzled by the comment: actually, there is nothing that prevents an optimiser to re-write the code to

Code:

            if (tmp_ptr) tmp_ptr->unref();
            if (_ptr) _ptr->ref();




or


Code:

           _ptr->unref();
            _ptr = rp._ptr;
            if (_ptr) _ptr->ref();




It seems to be that it has not happened, yet, respectively the scenario described in the comment did not occur, but this does not mean that it cannot happen, or?
Actually, I do not see a solution for the described scenario, so probably the comment should be removed or changed? But in this case the code can be simplified, too.

Cheers,
Hartwig[/code]

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







More information about the osg-users mailing list