Thanks J-S,
Yes, your first suggestion certainly fixes the problem (I tried it earlier!) and I agree, its probably and advantage to keep the interface the same. I think I just had a niggling feeling that it probably wasn't a good idea in principle to pass around C++ references to ref counted objects. I think your second suggestion however, might suffer the same fate as the original code because I suspect your second call to 'find' can still be passed a pointer to a deleted object, and so when the stl map template converts the dead pointer to a ref pointer before it does the find, so this probably still results in a call to ref for the deleted object and so incremenets the ref count in released space of the heap.
Cheers.
Chris.

----- Original Message ----- From: "Jean-Sébastien Guay" <[email protected]> To: "Chris Denham" <[email protected]>; "OpenSceneGraph Users" <[email protected]>
Sent: Tuesday, May 05, 2009 3:41 PM
Subject: Re: [osg-users] Possible dangling deleted pointer problem with Dragger/Manipulator


Hi Chris,

Why not:

bool CommandManager::disconnect(Dragger& dragger)
{
     osg::ref_ptr<Dragger> localDragger(&dragger);
   _draggerSelectionMap.erase(&dragger);
_draggerConstraintMap.erase(&dragger); // may be deleted at this point!!!!
   return true;
}

or just:

bool CommandManager::disconnect(Dragger& dragger)
{
if (_draggerSelectionMap.find(&dragger) != _draggerSelectionMap.end())
       _draggerSelectionMap.erase(&dragger);
if (_draggerConstraintMap.find(&dragger) != _draggerConstraintMap.end())
_draggerConstraintMap.erase(&dragger); // may be deleted at this point!!!!
   return true;
}

Then the interface doesn't change and it should be safe.

J-S
--
______________________________________________________
Jean-Sebastien Guay    [email protected]
                               http://www.cm-labs.com/
http://whitestar02.webhop.org/

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

Reply via email to