[osg-users] osg::ref ptr and memory leaks.

2013-06-07 Thread Roman Grigoriev
Hi,
Sorry for newbee question. But I found that my program memory usage grows up to 
200 k per cycle.
I try to find memory leak and maybe some guru look to this code portion and 
give me some advice. 
(sorry  because on linux when I copy code from qtcreator I've got some mess.)

Code:

  osg::ref_ptr  osg::MatrixTransform  viewnode_mtx;
osg::ref_ptr  SmokeParticleSystem  smoke_right;

class SmokeParticleSystem : public osg::Group {
private:
   // osgParticle::PointPlacer* placer;
public:
SmokeParticleSystem(osg::Group* scene, osg::MatrixTransform* 
mt,osg::Uniform *patt)  {
osgParticle::ParticleSystem* dustParticleSystem = new 
osgParticle::ParticleSystem;
osg::Geode *geode = new osg::Geode();
geode-addDrawable(dustParticleSystem);
this-addChild(geode);

// dustParticleSystem-setDefaultAttributes(smoke.rgb, false, false);
osgParticle::Particle smokeParticle;
smokeParticle.setSizeRange(osgParticle::rangef(1.5f,4.0f)); // meters
smokeParticle.setLifeTime(10); // seconds
smokeParticle.setMass(0.2f);
smokeParticle.setColorRange(osgParticle::rangev4(
osg::Vec4(1, 1.0f, 1.0f, 1.0f),
osg::Vec4(0.8, 0.8, 0.8, 0.0f)));
dustParticleSystem-setDefaultParticleTemplate(smokeParticle);
dustParticleSystem-setDefaultAttributesUsingShaders( smoke.rgb, 
false, 0 );
osgParticle::ParticleSystemUpdater *dustSystemUpdater = new 
osgParticle::ParticleSystemUpdater;
dustSystemUpdater-addParticleSystem(dustParticleSystem);
this-addChild(dustSystemUpdater);
osgParticle::ModularEmitter *emitter = new osgParticle::ModularEmitter;
emitter-setParticleSystem(dustParticleSystem);
this-addChild(emitter);
dustRate =
static_castosgParticle::RandomRateCounter 
*(emitter-getCounter());
dustRate-setRateRange(20,30);
osgParticle::PointPlacer* placer =
static_castosgParticle::PointPlacer *(emitter-getPlacer());
this-setUpdateCallback(new EmitterUpdateCallback(placer, mt));
osgParticle::RadialShooter* shooter =
static_castosgParticle::RadialShooter 
*(emitter-getShooter());
shooter-setThetaRange(-osg::PI_4/8.0, osg::PI_4/8.0 ); // radians, 
relative to Z axis.
shooter-setInitialSpeedRange(0,20); // meters/second
emitter-setShooter(shooter);
osgParticle::ModularProgram *program = new osgParticle::ModularProgram;
program-setParticleSystem(dustParticleSystem);
scene-addChild(program);

osgParticle::FluidFrictionOperator *airFriction = new 
osgParticle::FluidFrictionOperator;
airFriction-setFluidToAir();
airFriction-setWind(osg::Vec3(0.25, 0, 0));
program-addOperator(airFriction);

osgParticle::AccelOperator *accelUp = new osgParticle::AccelOperator;
accelUp-setToGravity(-0.2f);
program-addOperator(accelUp);

osg::StateSet* stateset = dustParticleSystem-getOrCreateStateSet();
stateset-setAttribute(program_data-particle_program);
osg::Uniform* baseTextureSampler = new osg::Uniform(baseTexture,0);
stateset-addUniform(baseTextureSampler);
stateset-addUniform(patt);
}
osgParticle::RandomRateCounter *dustRate;
};



At the begining of every cycle I clean root this way.

Code:

  viewnode_mtx-removeChildren(0,viewnode_mtx-getNumChildren());



when I need to add particle system to scenery root I use.

Code:

  this-smoke_right = new SmokeParticleSystem(this-viewnode, 
this-pat_r.get(),this-point_att_u);
this-smoke_right-setNodeMask(IS_VISIBLE_MASK);
this-smoke_right-setName(smoke);
this-viewnode_mtx-addChild( this-smoke_right);


  
Could you please comment if memory freed when I remove children or not. And 
maybe I should use ref_ptr on smokeparticlesystem. 
Thank you!
Cheers,
Roman

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





___
osg-users mailing list
osg-users@lists.openscenegraph.org
http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org


Re: [osg-users] osg::ref ptr and memory leaks.

2013-06-07 Thread Robert Osfield
Hi Roman,

The scene graph uses ref_ptr internally to make sure that it's
memory management is robust.  In your own application you should also
use ref_ptr for almost all cases where you would have used a C
pointer before.  Exceptions are cases such as inner loops where you
don't want to pay the cost of ref/unref and know that the object will
safely not be deleted while you are accessing it.  Another exception
would be where you would have a circular reference, here the parent
has to have the reference, while the child has a C pointer or an
observer_ptr.

Whether you actually have a memory leak or not I can not say, but I
wouldn't assume it.  Particular systems will general particles and
until they reach a steady state will keep increase memory usage.  The
rate of memory usage increase and the point it reaches steady state
(starts recycling expired particles) depends upon your settings so try
adjust the settings to generate less particles and see if that affect
things.

Robert.

On 7 June 2013 11:32, Roman Grigoriev grigor...@gosniias.ru wrote:
 Hi,
 Sorry for newbee question. But I found that my program memory usage grows up 
 to 200 k per cycle.
 I try to find memory leak and maybe some guru look to this code portion and 
 give me some advice.
 (sorry  because on linux when I copy code from qtcreator I've got some mess.)

 Code:

   osg::ref_ptr  osg::MatrixTransform  viewnode_mtx;
 osg::ref_ptr  SmokeParticleSystem  smoke_right;

 class SmokeParticleSystem : public osg::Group {
 private:
// osgParticle::PointPlacer* placer;
 public:
 SmokeParticleSystem(osg::Group* scene, osg::MatrixTransform* 
 mt,osg::Uniform *patt)  {
 osgParticle::ParticleSystem* dustParticleSystem = new 
 osgParticle::ParticleSystem;
 osg::Geode *geode = new osg::Geode();
 geode-addDrawable(dustParticleSystem);
 this-addChild(geode);

 // dustParticleSystem-setDefaultAttributes(smoke.rgb, false, 
 false);
 osgParticle::Particle smokeParticle;
 smokeParticle.setSizeRange(osgParticle::rangef(1.5f,4.0f)); // meters
 smokeParticle.setLifeTime(10); // seconds
 smokeParticle.setMass(0.2f);
 smokeParticle.setColorRange(osgParticle::rangev4(
 osg::Vec4(1, 1.0f, 1.0f, 1.0f),
 osg::Vec4(0.8, 0.8, 0.8, 0.0f)));
 dustParticleSystem-setDefaultParticleTemplate(smokeParticle);
 dustParticleSystem-setDefaultAttributesUsingShaders( smoke.rgb, 
 false, 0 );
 osgParticle::ParticleSystemUpdater *dustSystemUpdater = new 
 osgParticle::ParticleSystemUpdater;
 dustSystemUpdater-addParticleSystem(dustParticleSystem);
 this-addChild(dustSystemUpdater);
 osgParticle::ModularEmitter *emitter = new 
 osgParticle::ModularEmitter;
 emitter-setParticleSystem(dustParticleSystem);
 this-addChild(emitter);
 dustRate =
 static_castosgParticle::RandomRateCounter 
 *(emitter-getCounter());
 dustRate-setRateRange(20,30);
 osgParticle::PointPlacer* placer =
 static_castosgParticle::PointPlacer *(emitter-getPlacer());
 this-setUpdateCallback(new EmitterUpdateCallback(placer, mt));
 osgParticle::RadialShooter* shooter =
 static_castosgParticle::RadialShooter 
 *(emitter-getShooter());
 shooter-setThetaRange(-osg::PI_4/8.0, osg::PI_4/8.0 ); // radians, 
 relative to Z axis.
 shooter-setInitialSpeedRange(0,20); // meters/second
 emitter-setShooter(shooter);
 osgParticle::ModularProgram *program = new 
 osgParticle::ModularProgram;
 program-setParticleSystem(dustParticleSystem);
 scene-addChild(program);

 osgParticle::FluidFrictionOperator *airFriction = new 
 osgParticle::FluidFrictionOperator;
 airFriction-setFluidToAir();
 airFriction-setWind(osg::Vec3(0.25, 0, 0));
 program-addOperator(airFriction);

 osgParticle::AccelOperator *accelUp = new osgParticle::AccelOperator;
 accelUp-setToGravity(-0.2f);
 program-addOperator(accelUp);

 osg::StateSet* stateset = dustParticleSystem-getOrCreateStateSet();
 stateset-setAttribute(program_data-particle_program);
 osg::Uniform* baseTextureSampler = new osg::Uniform(baseTexture,0);
 stateset-addUniform(baseTextureSampler);
 stateset-addUniform(patt);
 }
 osgParticle::RandomRateCounter *dustRate;
 };



 At the begining of every cycle I clean root this way.

 Code:

   viewnode_mtx-removeChildren(0,viewnode_mtx-getNumChildren());



 when I need to add particle system to scenery root I use.

 Code:

   this-smoke_right = new SmokeParticleSystem(this-viewnode, 
 this-pat_r.get(),this-point_att_u);
 this-smoke_right-setNodeMask(IS_VISIBLE_MASK);
 this-smoke_right-setName(smoke);
 this-viewnode_mtx-addChild( 

Re: [osg-users] osg::ref ptr and memory leaks.

2013-06-07 Thread Roman Grigoriev
Hi, Robert
Thanx for your quick reply, Is there any way to calculate memory usage of my 
custom particle system class? If I query this-smoke_right.valid() it returns 
false, when i remove children  but As far as you've seen I don't have 
destructor on my node and I suspect that internal geode and particle system 
doesn't free from memory. Maybe It's better in this situation not to use 
ref_ptr? Or maybe create particle system in the beginning and attach already 
made every time but  in this way I have to clean all particle system variables.
Thank you!
Cheers,
Roman

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





___
osg-users mailing list
osg-users@lists.openscenegraph.org
http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org


Re: [osg-users] osg::ref ptr and memory leaks.

2013-06-07 Thread Bram Vaessen
Another idea might be to use valgrind, memory doctor or some similar program to 
detect actuall memory leaks.

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





___
osg-users mailing list
osg-users@lists.openscenegraph.org
http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org