<div dir="ltr">Kristofer,<div><br></div><div>Yes. The semantics of observer_ptr guarantee the existence of the target object ONLY after calling lock(). Only by calling lock() can you know for certain that the target object exists; calling get() is insufficient because get() will return a raw pointer that may or may not actually still point to an existing object. Avoid observer_ptr::get() and always use the lock() idiom.</div><div><br></div><div>lock() populates a true ref_ptr with the object pointer. If there were no pre-existing references to the object, that new ref_ptr would destroy the object when it went out of scope -- that would be unexpected and undesirable behavior. So returning false makes sense.</div><div><br></div><div>I suspect that having a class creating an observer to itself is a design flaw :)</div><div><br clear="all"><div><div dir="ltr" class="gmail_signature" data-smartmail="gmail_signature"><div dir="ltr"><div>Glenn Waldron / osgEarth</div></div></div></div><br></div><br><div class="gmail_quote"><div dir="ltr">On Wed, Oct 24, 2018 at 8:59 AM Kristofer Krus <<a href="mailto:kristofer.krus@liu.se">kristofer.krus@liu.se</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Hi Robert,<br>
<br>
Thanks. I'm having the problem that an observer_ptr that is assigned a pointer to an object that hasn't yet been assigned to a ref_ptr looks like it is invalid when I try to lock it (but not when I call the get method, so there is a discrepancy between observer_ptr::lock and observer_ptr::get in that sense). When the pointer is also assigned to a ref_ptr so that the reference count in the referenced object goes up, the lock method starts to return true instead of false.<br>
<br>
Here is what I do: In the constructor of a class that is referencable, I create an observer_ptr, obs, that is assigned the this-pointer—pointing at object A, which is the object being constructed—and I send obs to another object B that wants to observe A. at this point, it looks like obs is invalid. Directly after A has been constructed—by using the new-keyword—its memory address is assigned to a ref_ptr and obs.lock suddenly starts to succeed.<br>
<br>
Is this desirable behavior? I mean, the object exists, right? Why would otherwise get() succeed? Also, if you create an obs_ptr to an object located on the heap, that won't work as expected either when you try to lock it.<br>
<br>
Cheers,<br>
Kristofer<br>
<br>
------------------<br>
Read this topic online here:<br>
<a href="http://forum.openscenegraph.org/viewtopic.php?p=75114#75114" rel="noreferrer" target="_blank">http://forum.openscenegraph.org/viewtopic.php?p=75114#75114</a><br>
<br>
<br>
<br>
<br>
<br>
_______________________________________________<br>
osg-users mailing list<br>
<a href="mailto:osg-users@lists.openscenegraph.org" target="_blank">osg-users@lists.openscenegraph.org</a><br>
<a href="http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org" rel="noreferrer" target="_blank">http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org</a><br>
</blockquote></div></div>