|
问题是这样子的,编译OSG里面,有个osgviewerMFC的例子,我在这个例子里面添加了一个自定义的事件处理类,
其定义如下:
#include "StdAfx.h"
#include "ickHandler.h"
PickHandler:ickHandler(void):_mx(0.0f),_my(0.0f)
{
}
PickHandler::~PickHandler(void)
{
}
bool PickHandler::handle(const osgGA::GUIEventAdapter &ea,osgGA::GUIActionAdapter &aa)
{
osg::ref_ptr<osgViewer::View>view = dynamic_cast<osgViewer::View*>(&aa);
if(!view) return false;
switch(ea.getEventType())
{
case(osgGA::GUIEventAdapter::PUSH):
{
_mx = ea.getX();
_my = ea.getY();
break;
}
case(osgGA::GUIEventAdapter::RELEASE):
{
if(_mx == ea.getX() && _my == ea.getY() )
{
pick(view.get(),ea.getX(),ea.getY());
}
break;
}
default:
break;
}
return false;
}
void PickHandler::pick(osg::ref_ptr<osgViewer::View>view,float x,float y)
{
osg::ref_ptr<osg::Node> node = new osg::Node();
osg::ref_ptr<osg::Group> parent = new osg::Group();
osgUtil:ineSegmentIntersector::Intersections intersections;
if(view->computeIntersections(x,y,intersections))
{
osgUtil::LineSegmentIntersector::Intersection intersection = *intersections.begin();
osg::NodePath& nodePath = intersection.nodePath;
node = (nodePath.size()>= 1)?nodePath[nodePath.size()-1]:0;
parent = (nodePath.size()>= 2)?dynamic_cast<osg::Group*>(nodePath[nodePath.size()-2]):0;
}
if(parent.get() && node.get())
{
osg::ref_ptr<osgFX::Scribe>parentAsScribe = dynamic_cast<osgFX::Scribe*>(parent.get());
if(!parentAsScribe)
{
osg::ref_ptr<osgFX::Scribe> scribe = new osgFX::Scribe();
scribe->addChild(node.get());
parent->replaceChild(node.get(),scribe.get());
}
else
{
osg::Node::ParentList parentlist = parentAsScribe->getParents();
for(osg::Node::ParentList::iterator it = parentlist.begin();
it!=parentlist.end();++it)
{
(*it)->replaceChild(parentAsScribe.get(),node.get());
}
}
}
}
但是我将这个事件处理加入到场景里//Add a Event Handler to the viewer
mViewer->addEventHandler(new PickHandler());
运行时缺出现了,内存错误,然后我跟踪了下源代码,发现问题在这里viewer类的如下位置:
for(osgGA::EventQueue::Events::iterator itr = events.begin();
itr != events.end();
++itr)
{
osgGA::GUIEventAdapter* event = itr->get();
for(EventHandlers::iterator hitr = _eventHandlers.begin();
hitr != _eventHandlers.end();
++hitr)
{
(*hitr)->handleWithCheckAgainstIgnoreHandledEventsMask( *event, *this, 0, 0);
}
}
这段代码是将用户添加的时间处理器进行一些初始化的工作,_eventHandlers是一个List容器,
我这个例子程序里面,以及添加了2个事件处理,所以遍历这个容器时,当遍历第三次时,hitr迭代器应该是到了list尾部,但是
我调试的时候发现,其实第三次时list的值是0xfeeefeee,不等于_eventHandlers.end(),由于指针无效,此时再去执行下面的函数调用,就会出现list iterator not incrementable的错误,
请知道的大哥指点下,这是OSG源码的问题还是我这个编译的问题,另外我试了下32位和64位编译的osg都是这个现象,
另外就是我的系统是64的WIN7系统。
|
|