[osg-users] osg::observer_ptr and osg::ref_ptr

Kristofer Krus kristofer.krus at liu.se
Wed Oct 24 05:59:29 PDT 2018

Hi Robert,

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.

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.

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.


Read this topic online here:

More information about the osg-users mailing list