<div dir="ltr"><div><div><div>Hi Hartwig,<br><br></div>As far as I can tell the code and comment is correct, the unref afterwards is to catch cases like:<br><br></div><div><br></div><div>osg::Group* parent = new osg::Group;</div><div>osg::Node* child = new osg::Geode;</div><div>parent->addChild(child);</div><div><br></div>osg::ref_ptr<osg::Node> ptr = parent; // takes a reference to pararent<br><br></div>ptr = parent->getChild(0); // should take a references to child and then unref the parent deleting it<br><br><br><div>Now think what would happen if you unref first... it would delete the parent and it's child, then you'd assign a dangling pointer.</div><div><br></div><div>If the compiler's optimizer is breaking this then... it's a terrible compiler. It does highlight how subtle ref counting can be, and how much optimizers can't just blindly do certain types of optimization like re-ordering.<br></div><div><br><div>Robert.<br></div><div><br></div></div></div><div class="gmail_extra"><br><div class="gmail_quote">On 24 October 2017 at 23:34, Hartwig Wiesmann <span dir="ltr"><<a href="mailto:hartwig.wiesmann@skywind.eu" target="_blank">hartwig.wiesmann@skywind.eu</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Hi,<br>
<br>
in ref_ptr you find the following code:<br>
<br>
<br>
Code:<br>
<br>
template<class Other> void assign(const ref_ptr<Other>& rp)<br>
{<br>
if (_ptr==rp._ptr) return;<br>
T* tmp_ptr = _ptr;<br>
_ptr = rp._ptr;<br>
if (_ptr) _ptr->ref();<br>
// unref second to prevent any deletion of any object which might<br>
// be referenced by the other object. i.e rp is child of the<br>
// original _ptr.<br>
if (tmp_ptr) tmp_ptr->unref();<br>
}<br>
<br>
<br>
<br>
<br>
I was puzzled by the comment: actually, there is nothing that prevents an optimiser to re-write the code to<br>
<br>
Code:<br>
<br>
if (tmp_ptr) tmp_ptr->unref();<br>
if (_ptr) _ptr->ref();<br>
<br>
<br>
<br>
<br>
or<br>
<br>
<br>
Code:<br>
<br>
_ptr->unref();<br>
_ptr = rp._ptr;<br>
if (_ptr) _ptr->ref();<br>
<br>
<br>
<br>
<br>
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?<br>
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.<br>
<br>
Cheers,<br>
Hartwig[/code]<br>
<br>
------------------<br>
Read this topic online here:<br>
<a href="http://forum.openscenegraph.org/viewtopic.php?p=72242#72242" rel="noreferrer" target="_blank">http://forum.openscenegraph.<wbr>org/viewtopic.php?p=72242#<wbr>72242</a><br>
<br>
<br>
<br>
<br>
<br>
______________________________<wbr>_________________<br>
osg-users mailing list<br>
<a href="mailto:osg-users@lists.openscenegraph.org">osg-users@lists.<wbr>openscenegraph.org</a><br>
<a href="http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org" rel="noreferrer" target="_blank">http://lists.openscenegraph.<wbr>org/listinfo.cgi/osg-users-<wbr>openscenegraph.org</a><br>
</blockquote></div><br></div>