查看: 1785|回复: 3

求助,MFC中释放粒子节点,程序崩溃了

[复制链接]

该用户从未签到

发表于 2013-1-8 23:14:07 | 显示全部楼层 |阅读模式
本帖最后由 sy2178668 于 2013-1-8 23:18 编辑

我在osg自带的osgviewerMFC程序里加了一个粒子效果,运行没有问题,当一卸载后,就有可能会崩溃,百思不得其解,望高手解答,下附代码和崩溃截图
这是创建粒子的函数:
  1. osg::ref_ptr<osg::Group> CreateParticle::createInitPar()
  2. {
  3.         osg::ref_ptr<osg::Group> parRoot = new osg::Group;

  4.         osgParticle::Particle ptemplate;
  5.         ptemplate.setLifeTime(2.0);
  6.         ptemplate.setSizeRange(osgParticle::rangef(0.1,0.1));
  7.         ptemplate.setAlphaRange(osgParticle::rangef(1.0,0.0));
  8.         ptemplate.setColorRange(osgParticle::rangev4(osg::Vec4(1.0,1.0,1.0,1.0),osg::Vec4(1.0,1.0,1.0,0.0)));
  9.         ptemplate.setRadius(0.05);
  10.         ptemplate.setMass(0.05);

  11.         osg::ref_ptr<osgParticle::ParticleSystem> ps = new osgParticle::ParticleSystem;
  12.         ps->setDefaultAttributes("Images/smoke.rgb",false,false);
  13.         ps->setDefaultParticleTemplate(ptemplate);

  14.         osg::ref_ptr<osgParticle::ModularEmitter> emitter = new osgParticle::ModularEmitter;
  15.         emitter->setLifeTime(2.0);
  16.         emitter->setStartTime(0.0);
  17.         emitter->setEndless(true);
  18.         emitter->setParticleSystem(ps.get());

  19.         osg::ref_ptr<osgParticle::RandomRateCounter> counter = new osgParticle::RandomRateCounter;
  20.         counter->setRateRange(10000.0,10000.0);
  21.         emitter->setCounter(counter.get());

  22.         osg::ref_ptr<osgParticle::SectorPlacer> placer = new osgParticle::SectorPlacer;
  23.         placer->setCenter(osg::Vec3(0.0,0.0,0.0));
  24.         placer->setRadiusRange(0.0,0.05);
  25.         emitter->setPlacer(placer.get());

  26.         osg::ref_ptr<osgParticle::RadialShooter> shooter = new osgParticle::RadialShooter;
  27.         shooter->setInitialSpeedRange(40.0,70.0);
  28.         emitter->setShooter(shooter.get());

  29.         osg::ref_ptr<osgParticle::ModularProgram> program = new osgParticle::ModularProgram;
  30.         program->setParticleSystem(ps);

  31.         osg::ref_ptr<osgParticle::AccelOperator> ao = new osgParticle::AccelOperator;
  32.         ao->setToGravity(1);
  33.         program->addOperator(ao);

  34.         osg::ref_ptr<osgParticle::FluidFrictionOperator> ffo = new osgParticle::FluidFrictionOperator;
  35.         ffo->setFluidViscosity(1.8e-5);
  36.         ffo->setFluidDensity(1.2929);
  37.         ffo->setWind(osg::Vec3(0.f,0.f,0.f));
  38.         program->addOperator(ffo);

  39.         osg::ref_ptr<osgParticle::ParticleSystemUpdater> psu = new osgParticle::ParticleSystemUpdater;
  40.         psu->addParticleSystem(ps.get());
  41.        
  42.         osg::Geode* geode = new osg::Geode;
  43.         geode->addDrawable(ps.get());
  44.         parRoot->addChild(emitter.get());
  45.         parRoot->addChild(program.get());
  46.         parRoot->addChild(geode);

  47.         parRoot->addChild(psu.get());

  48.         return parRoot;
  49. }
复制代码
这是移除和添加粒子代码,其中parGroup是专门存放粒子的组节点,其父节点是场景总节点:
  1. parGroup->removeChildren(0, parGroup->getNumChildren());
  2.         parGroup->addChild(cp.createInitPar().get());
复制代码
这是运行截图:
QQ截图20130108225548.png

这是崩溃的位置。。。:
崩溃位置1:
QQ截图20130108225830.png
崩溃位置2:
QQ截图20130108230107.png
崩溃位置3:
QQ截图20130108230219.png

我怀疑出现这个问题有可能是以下几个原因:
1:粒子释放的时候释放不干净;
2:由于每帧更新的粒子数目太多,所以释放会出问题;
3:osg渲染是在
  1. m_ThreadHandle = (HANDLE)_beginthread(&CCoreOSG::Render, 0, m_OSG) ;
复制代码
线程里渲染的,是不是释放和这个线程有冲突,因为经常死在这里。。。

很纳闷,求解答

该用户从未签到

 楼主| 发表于 2013-1-8 23:20:33 | 显示全部楼层
求各位高手解答

该用户从未签到

发表于 2013-1-9 07:51:39 | 显示全部楼层
使用新的osgMFC openthreads

该用户从未签到

 楼主| 发表于 2013-1-9 10:26:11 | 显示全部楼层
liuzhiyu123 发表于 2013-1-9 07:51
使用新的osgMFC openthreads

您好,您的意思是把渲染线程用OpenThreads实现,而不用_beginthread实现吗??谢谢
您需要登录后才可以回帖 登录 | 注册

本版积分规则

OSG中国官方论坛-有您OSG在中国才更好

网站简介:osgChina是国内首个三维相关技术开源社区,旨在为国内更多的技术开发人员提供最前沿的技术资讯,为更多的三维从业者提供一个学习、交流的技术平台。

联系我们

  • 工作时间:09:00--18:00
  • 反馈邮箱:1315785073@qq.com
快速回复 返回顶部 返回列表