Tim Moore wrote:
On Sat, Feb 6, 2010 at 4:07 AM, Kevin Wilder <[email protected] <mailto:[email protected]>> wrote:
    "Be careful when returning the address of an object from a function.
    If you do this incorrectly, the ref_ptr<> storing the memory address
    could go out of scope before the address is placed on the return stack."

This is talking about the following anti-pattern:
osg::Node* func()
{
  osg::ref_ptr<Node> result;
...
  return result.get(); // result gets deleted!
}

Instead of returning "result.get()" you need to return "result.release()" which prevents the ref_ptr destructor from deleting the object.

Yes, "result.release()" is the correct way to return a Referenced address stored in a local ref_ptr<> without causing the memory to be deleted.

The current edition of the Quick Start Guide (QSG) doesn't use release() and instead advises returning a ref_ptr<> instead of a regular C pointer. This works but is somewhat inefficient because it creates a ref_ptr<> that typically would exist for no more that a few instructions. release() is better, and the QSG example code has been updated to use release(), but the actual QSG itself still needs rewriting.
   -Paul

_______________________________________________
osg-users mailing list
[email protected]
http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org

Reply via email to