2009/5/28 Jason Daly <jd...@ist.ucf.edu>: > Rabbi Robinson wrote: [snip] >> If later I call, ptr = NULL, it doesn't seem that there is anyway to tell >> node to decrement the reference count in node. > > This will decrement the reference count in node. If the reference count > goes to zero, it will be deleted. > > --"J" > > _______________________________________________ > osg-users mailing list > osg-users@lists.openscenegraph.org > http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org >
Hi, small correction, if you don't misunderstand; that will not decrease reference count, since ptr is void* and has no connection whatsoever with pointee (osg::Referenced). In following case: osg::ref_ptr<osg::Node> pnode = new osg::Node; void* ptr = pnode; ptr = NULL; (this is what I understood) Rabbi, once you get raw pointer from smart pointer, you take responsibility of reference counting (and of course, C++ scoping rules). You may pass that pointer around, but you should be careful with reference counting and scope that osg::ref_ptr<T> was declared. By default, OSG_USE_REF_PTR_IMPLICIT_OUTPUT_CONVERSION is defined (as far as I can see). This means there is an implicit conversion from ref_ptr to T (and hence, to types has implicit conversion from T, e.g. pointer to void) - you can pass an osg::ref_ptr<T> to a function that expects T* and osg::ref_ptr<T> (even a void*) will return its raw pointer without modifying reference count. In safe C++ programming practice, this usually is taken dangerous, if not bad, practice; in such cases, conversion must be explicitly expressed so that programmer knows and takes responsibility. After your ptr (which was declared as pointer to void) is assigned to NULL, ptr is assigned to NULL; pnode has no knowledge whatsoever of raw pointer you obtained after that line you perform assignment; you might have somehow overwritten it, somehow deleted that (in our case, it isn't possible but could well be possible). If you somehow store returned raw pointer and try to use it beyond the scope that surrounds pnode, you may crash your app, since object might have been deleted by osg::ref_ptr<T>. e.g. class MyClass { osg::Node* m_pNode; void foo(); void bar(); ... }; void MyClass::foo() { osg::ref_ptr<osg::Node> pnode = new osg::Node; m_pNode = pNode; } // pnode dies here void MyClass::bar() { traverseNode(m_pNode); // expect anything but correct functionality } Bottom line is; use smart pointers instead of raw pointers and be explicit about conversions. Respect scope of smart pointer, because it can't know what you do with the raw pointer once you obtain it. Ismail _______________________________________________ osg-users mailing list osg-users@lists.openscenegraph.org http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org