Hi Tim, 

  I understand what you said and I tried it, it doesn't seem to be the problem. 
My function is based in the one you can see at page  37 of the quick start 
guide:


Code:
There are multiple solutions to the dilemma of how to return a Referenced 
object address. The method employed in this book’s example code is to return a 
ref_ptr<> storing the address, as the code below illustrates.
osg::ref_ptr<osg::Group> createGroup()
{
osg::ref_ptr<osg::Group> grp = new osg::Group;
// Return the new Group’s address. This stores the Group // address in a 
ref_ptr<> and places the ref_ptr<> on the // call stack as the return value.
return grp.get();
}



So I thought the process was not only correct but also the usual way. I've been 
revising the osgkeyboardmouse example and I've applied the way in which it 
deletes the scribe node it uses in order to mark the selected nodes. If i do 
that, it works, but I thought there is a memory leak. The new handle method is 
as follows:



Code:

void PickHandler::removeSelectedMarkIfNeeded(void)
{
    if (_selectionBox.valid() == true)
    {
        osg::Node::ParentList parents = _selectionBox->getParents();

        for(osg::Node::ParentList::iterator pitr = parents.begin(); pitr != 
parents.end(); ++pitr)
        {
            osg::Group* parent = *pitr;
            parent->removeChild(_selectionBox);
        }
        
        _selected = false;
    }
}

bool PickHandler::handle(const osgGA::GUIEventAdapter &ea, 
osgGA::GUIActionAdapter &aa)
{
    osgViewer::Viewer *viewer;
    
    viewer = dynamic_cast<osgViewer::Viewer *>(&aa);
    
    if (ea.getEventType() == osgGA::GUIEventAdapter::RELEASE)
    {
        osg::ref_ptr<osg::Group> rootNode;
        
        rootNode = dynamic_cast<osg::Group *>(viewer->getSceneData());
        
        if (ea.getButton() == osgGA::GUIEventAdapter::LEFT_MOUSE_BUTTON)
        {
            osg::ref_ptr<osgUtil::LineSegmentIntersector> intersector;
            osgUtil::IntersectionVisitor *iv;

            intersector = new 
osgUtil::LineSegmentIntersector(osgUtil::Intersector::WINDOW, ea.getX(), 
ea.getY());
            
            iv = new osgUtil::IntersectionVisitor(intersector.get());
            iv->setTraversalMask(~0x1);
            
            viewer->getCamera()->accept(*iv);
            
            if (intersector->containsIntersections() == true)
            {
                const osgUtil::LineSegmentIntersector::Intersection &result = 
*(intersector->getIntersections().begin());
                osg::BoundingBox bb;
                osg::Vec3 worldCenter;
                
                bb = result.drawable->getBound();
                worldCenter = bb.center() * 
osg::computeLocalToWorld(result.nodePath);
                
                if (_selectionBox == NULL)
                    _selectionBox = getOrCreateSelectionBox();
                
                _selectionBox->setMatrix(
                            osg::Matrix::scale(bb.xMax() - bb.xMin(), bb.yMax() 
- bb.yMin(), bb.zMax() - bb.zMin())
                            *
                            osg::Matrix::translate(worldCenter));
                
                rootNode->addChild(_selectionBox.get());
                
                _selected = true;
            }
            else 
            {
                removeSelectedMarkIfNeeded();
            }
        }
        else if (ea.getButton() == osgGA::GUIEventAdapter::RIGHT_MOUSE_BUTTON)
        {
            removeSelectedMarkIfNeeded();
        }
    }
    
    return false; // Event not maganed, false to continue to be processing by 
another event handlers
}



Curiously, if after deleting all the parents in the 
removeSelectedMarkIfNeeded() method  i do:


Code:

void PickHandler::removeSelectedMarkIfNeeded(void)
{
    if (_selectionBox.valid() == true)
    {
        osg::Node::ParentList parents = _selectionBox->getParents();

        for(osg::Node::ParentList::iterator pitr = parents.begin(); pitr != 
parents.end(); ++pitr)
        {
            osg::Group* parent = *pitr;
            parent->removeChild(_selectionBox);
        }
        
        _selectedBox = NULL; //THIS IS THE DIFFERENCE
        _selected = false;
    }
}




In the next cull the viewer crashes, but i deleted all the parents! Who is 
referencing it? I will try to print the graph to a file to analyze it.

 Another question that surprise me is that i expected _selectedBox to have only 
one parent but in fact, it has more than one. 

I believe i'm doing something terribly wrong but because it works in my other 
computer i don't know what.

Thank you, Manuel.

------------------
Read this topic online here:
http://forum.openscenegraph.org/viewtopic.php?p=48826#48826





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

Reply via email to