Hi Manuel,

I'm not sure about if the problem could be that you are removing an object
that is being traversed. I mean is safe to remove objects before and after
viewer.frame(). During traversals you must not remove objects because could
invalidate iterators and give you that kind of crashes.

Try the next approach:
- 'Save a reference' to the node to remove in the Handler.
- Unroll the viewer.run() in while(!viewer.done())  { viewer.frame(); }
- After the viewer.frame() call read the selected object and remove it from
the graph.

Hope it helps.

Regards,
Rafa.


2012/7/12 Manuel Fernández <[email protected]>

> Hi,
>
> As mentioned in other post i've struggling myself trying to get OSG 3.0.1
> working on my iMac. I've have simplified the problem a lot, so here it is.
> This is a simple pick handler (base on the one that you can see in OSG
> cookbook):
>
>
> Code:
> class PickHandler: public osgGA::GUIEventHandler
> {
>     public:
>
>         PickHandler(osg::ref_ptr<osg::Group> rootNode,
> osg::ref_ptr<osg::Camera> camera);
>         virtual bool handle(const osgGA::GUIEventAdapter &ea,
> osgGA::GUIActionAdapter &aa);
>
>     private:
>
>         osg::MatrixTransform *getOrCreateSelectionBox();
>
>     protected:
>
>         osg::ref_ptr<osg::Group> _rootNode;
>         osg::ref_ptr<osg::Camera> _camera;
>
>         osg::ref_ptr<osg::MatrixTransform> _selectionBox;
>
>         bool withFirstPoint;
>         double x1, y1, z1;
>         osg::ref_ptr<osg::Geode> _temporayDecoration;
> };
>
> PickHandler::PickHandler(osg::ref_ptr<osg::Group> rootNode,
> osg::ref_ptr<osg::Camera> camera)
> {
>     _rootNode = rootNode;
>     _camera = camera;
>
>     _selectionBox = NULL;
>
>     withFirstPoint = false;
>     x1 = 0.;
>     y1 = 0.;
>     z1 = 0.;
>     _temporayDecoration = NULL;
> }
>
> osg::MatrixTransform *PickHandler::getOrCreateSelectionBox()
> {
>     if (_selectionBox == NULL)
>     {
>         osg::ref_ptr<osg::Geode> geode;
>         osg::StateSet *ss;
>
>         geode = new osg::Geode;
>         geode->addDrawable(new osg::ShapeDrawable(new
> osg::Box(osg::Vec3(), 1.0f)));
>
>         _selectionBox = new osg::MatrixTransform;
>         _selectionBox->setNodeMask(0x1);
>         _selectionBox->addChild(geode.get());
>
>         ss = _selectionBox->getOrCreateStateSet();
>         ss->setMode(GL_LIGHTING, osg::StateAttribute::OFF);
>         ss->setAttributeAndModes(new
> osg::PolygonMode(osg::PolygonMode::FRONT_AND_BACK, osg::PolygonMode::LINE));
>     }
>
>     return _selectionBox.get();
> }
>
> 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)
>     {
>         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());
>             }
>             else
>             {
>                 if (_selectionBox != NULL)
>                 {
>                     _rootNode->removeChild(_selectionBox.get());
>                     _selectionBox = NULL;
>                 }
>             }
>         }
>         else if (ea.getButton() ==
> osgGA::GUIEventAdapter::RIGHT_MOUSE_BUTTON)
>         {
>              if (_selectionBox != NULL)
>              {
>                  _rootNode->removeChild(_selectionBox.get());
>                  _selectionBox = NULL;
>              }
>
>              withFirstPoint = false;
>         }
>     }
>
>     return false; // Event not maganed, false to continue to be processing
> by another event handlers
> }
>
>
>
>
> Well, this is the rest of the code:
>
>
> Code:
> static osg::ref_ptr<osg::Group> i_create_scenegraph5(void)
> {
>     osg::ref_ptr<osg::Group> root;
>     osg::ref_ptr<osg::Geode> geometry;
>     osg::ref_ptr<osg::ShapeDrawable> shape1, shape2, shape3;
>
>     shape1 = new osg::ShapeDrawable();
>     shape1->setShape(new osg::Box(osg::Vec3(-3, 0., 0.), 2., 2., 1.));
>     shape1->setColor(osg::Vec4(0., 0., 1., 1.));
>
>     shape2 = new osg::ShapeDrawable();
>     shape2->setShape(new osg::Sphere(osg::Vec3(3., 0., 0.), 1.f));
>
>     shape3 = new osg::ShapeDrawable();
>     shape3->setShape(new osg::Cone(osg::Vec3(0., 0., 0.), 1., 1.));
>
>     geometry = new osg::Geode();
>     geometry->addDrawable(shape1.get());
>     geometry->addDrawable(shape2.get());
>     geometry->addDrawable(shape3.get());
>
>     root = new osg::Group;
>     root->addChild(geometry.get());
>
>     return root.get();
> }
>
> int main(int argc, char **argv)
> {
>     osg::ref_ptr<osg::Group> root;
>     osg::ref_ptr<PickHandler> pickHandler;
>     osgViewer::Viewer viewer;
>
>     root = i_create_scenegraph5();
>     pickHandler = new PickHandler(root.get(), viewer.getCamera());
>
>     viewer.setSceneData(root.get());
>     viewer.addEventHandler(pickHandler.get());
>     viewer.setUpViewInWindow(100, 100, 400, 350);
>
>     return viewer.run();
> }
>
>
>
>
> When i click over one of the figures it works and the bounding box
> appears. After that, if i click in another figure, then i get the following
> error (with backtrace):
>
>
> Code:
> osgtest(30288,0x7fff7c4a7960) malloc: *** error for object 0x10ac534e0:
> pointer being freed was not allocated
> *** set a breakpoint in malloc_error_break to debug
> * thread #1: tid = 0x2703, 0x00007fff90cf96bc
> libsystem_c.dylib`malloc_error_break, stop reason = breakpoint 2.1
>     frame #0: 0x00007fff90cf96bc libsystem_c.dylib`malloc_error_break
>     frame #1: 0x00007fff90cf9805 libsystem_c.dylib`free + 318
>     frame #2: 0x00007fff959ca702
> libstdc++.6.dylib`std::string::_Rep::_M_dispose(std::allocator<char>
> const&) + 60
>     frame #3: 0x00007fff959ca740 libstdc++.6.dylib`std::basic_string<char,
> std::char_traits<char>, std::allocator<char> >::~basic_string() + 44
>     frame #4: 0x0000000107f35fc4 libosgd.80.dylib`osg::Object::~Object() +
> 116 at Object.cpp:45
>     frame #5: 0x0000000107f930c5 libosgd.80.dylib`osg::Shape::~Shape() +
> 21 at Shape.cpp:20
>     frame #6: 0x0000000107f93325 libosgd.80.dylib`osg::Box::~Box() + 21 at
> Shape.cpp:36
>     frame #7: 0x0000000107f93305 libosgd.80.dylib`osg::Box::~Box() + 21 at
> Shape.cpp:35
>     frame #8: 0x0000000107f932a8 libosgd.80.dylib`osg::Box::~Box() + 24 at
> Shape.cpp:35
>     frame #9: 0x0000000107f742da
> libosgd.80.dylib`osg::Referenced::signalObserversAndDelete(bool, bool)
> const + 298 at Referenced.cpp:323
>     frame #10: 0x0000000107ac5734
> libosgAnimationd.80.dylib`osg::Referenced::unref() const + 84 at
> Referenced:198
>     frame #11: 0x0000000107e35b10
> libosgd.80.dylib`osg::ref_ptr<osg::Shape>::~ref_ptr() + 48 at ref_ptr:35
>     frame #12: 0x0000000107e2e525
> libosgd.80.dylib`osg::ref_ptr<osg::Shape>::~ref_ptr() + 21 at ref_ptr:35
>     frame #13: 0x0000000107e294da
> libosgd.80.dylib`osg::Drawable::~Drawable() + 186 at Drawable.cpp:273
>     frame #14: 0x0000000107fa4dfa
> libosgd.80.dylib`osg::ShapeDrawable::~ShapeDrawable() + 58 at
> ShapeDrawable.cpp:1938
>     frame #15: 0x0000000107fa4db5
> libosgd.80.dylib`osg::ShapeDrawable::~ShapeDrawable() + 21 at
> ShapeDrawable.cpp:1937
>     frame #16: 0x0000000107fa4d58
> libosgd.80.dylib`osg::ShapeDrawable::~ShapeDrawable() + 24 at
> ShapeDrawable.cpp:1937
>     frame #17: 0x0000000107f742da
> libosgd.80.dylib`osg::Referenced::signalObserversAndDelete(bool, bool)
> const + 298 at Referenced.cpp:323
>     frame #18: 0x0000000107ac5734
> libosgAnimationd.80.dylib`osg::Referenced::unref() const + 84 at
> Referenced:198
>     frame #19: 0x0000000107e5954d
> libosgd.80.dylib`osg::ref_ptr<osg::Drawable>::operator=(osg::Drawable*) +
> 125 at ref_ptr:58
>     frame #20: 0x0000000109bfe3a7
> libosgUtild.80.dylib`osgUtil::RenderLeaf::reset() + 55 at RenderLeaf:65
>     frame #21: 0x0000000109bf97e4
> libosgUtild.80.dylib`osgUtil::CullVisitor::reset() + 436 at
> CullVisitor.cpp:127
>     frame #22: 0x0000000109d9be70
> libosgUtild.80.dylib`osgUtil::SceneView::cullStage(osg::Matrixd const&,
> osg::Matrixd const&, osgUtil::CullVisitor*, osgUtil::StateGraph*,
> osgUtil::RenderStage*, osg::Viewport*) + 2096 at SceneView.cpp:910
>     frame #23: 0x0000000109d9b545
> libosgUtild.80.dylib`osgUtil::SceneView::cull() + 5557 at SceneView.cpp:845
>     frame #24: 0x000000010a65a2ad
> libosgViewerd.80.dylib`osgViewer::Renderer::cull() + 685 at Renderer.cpp:615
>     frame #25: 0x000000010a6d2000
> libosgViewerd.80.dylib`osgViewer::ViewerBase::renderingTraversals() + 8960
> at ViewerBase.cpp:793
>     frame #26: 0x000000010a6cfcf2
> libosgViewerd.80.dylib`osgViewer::ViewerBase::frame(double) + 178 at
> ViewerBase.cpp:645
>     frame #27: 0x000000010a6cfbbe
> libosgViewerd.80.dylib`osgViewer::ViewerBase::run() + 430 at
> ViewerBase.cpp:612
>     frame #28: 0x000000010a6c153c
> libosgViewerd.80.dylib`osgViewer::Viewer::run() + 220 at Viewer.cpp:375
>     frame #29: 0x0000000107a1b94a osgtest`main + 522 at main2.cpp:171
>     frame #30: 0x0000000107a103c4 osgtest`start + 52
>
>
>
>
> System: Apple iMac 21.5 Core i5 with Mac Os Lion 10.7.3 and Xcode 4.3.3.
> Compiler: Apple LLVM 3.1 (Clang, Clang++).
>
> I have tried:
>
>   - Read OSGQSG for memory management
>   - Read OSG Beginner' s guide
>   - Read OSG Cookbook 3.0
>   - Search through the forums (this is why a read a lot)
>   - Building after executing: sudo make uninstall.
>   - Building using GCC 4.2 (same result)
>   - Reinstalling Command Line Tools (February'12 version) (same result)
>   - Reinstalling XCode (i was becoming mad, same result)
>
> I've a mbp with the same OS, XCode and Command Line tools version.
> Everything works fine, but I suspect that simply the error doesn't appears
> on it because who knows why.
>
> I figure that the problem may have two causes:
>
>  - Some obscure build issue.
>  - I don't understand how to manage the scene graph memory.
>
> Remember that I want to delete the node from de scene, I don't want to
> hide it. I have made a simplified version of what I'm trying to do in order
> to get some guidance from the community.
>
> Yes, it's a long post, but I'm fighting with this problem since two weeks
> and I don't know how to solve it.
>
>
> Thank you in advance, Manuel.[/code]
>
> ------------------
> Read this topic online here:
> http://forum.openscenegraph.org/viewtopic.php?p=48819#48819
>
>
>
>
>
> _______________________________________________
> osg-users mailing list
> [email protected]
> http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org
>



-- 
Rafael Gaitán Linares
CTO at Mirage Technologies S.L - http://www.mirage-tech.com
gvSIG3D Developer - http://gvsig3d.blogspot.com
_______________________________________________
osg-users mailing list
[email protected]
http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org

Reply via email to