Hi Jan,
It is not clear to me why changing get() for release() solves your
crash. Releasing the smart pointer copies the underlying pointer into a
temporary and returns that, resetting the original pointer to 0, whereas
get() returns the underlying pointer directly. One way or another, the
same value is returned. So if the code was crashing, this change has
likely only masked the crash for you. Could you post the file where the
code crashes? Or at least a stack trace?
The problem is that returning anyRefPtr.get() causes the object to be
deleted before the value gets to the variable receiving it on the other
end. That happens because the sequence is actually:
1. put the value (the address of the pointer) into a return value register.
2. end of function: destroy local variables - here the ref count of the
ref_ptr goes to 0, hence the object pointed to is destroyed.
3. assign return value to variable receiving it in calling function.
So by the time the caller gets the pointer, the object it points to has
been destroyed. Returning anyRefPtr.release(); is the idiomatic solution
here - the function that created it does not need it anymore, but the
local ref_ptr instance should not manage its ref count because it needs
to be live when it gets to the caller.
I'm actually surprised you don't know about this. The OSG Quick Start
Guide explains this, and it's been discussed a lot before, and being an
experienced OSG developer I would have thought you would have run into
this before.
J-S
--
______________________________________________________
Jean-Sebastien Guay [email protected]
http://www.cm-labs.com/
http://whitestar02.webhop.org/
_______________________________________________
osg-submissions mailing list
[email protected]
http://lists.openscenegraph.org/listinfo.cgi/osg-submissions-openscenegraph.org