[osg-users] osgDotNet : Nodes adding to scene graph outside main() function scope

Christophe Medard christophe.medard at oktal.fr
Tue Sep 25 05:59:08 PDT 2007


Hi everyone,

I don't know if I was very clear in my last post. My problem is to implement in C# small sfx in nodes in the scenegraph...

The fact is that as soon as the Osg.Node (in the example the _root Osg.PositionAttitudeTransform) that is added to the scene is enterily monitored by a C# class (and not statically or locally defined in the main loop function), there seems to occur destruction on that Node being nonetheless regular in term of OSG reference count (the _root is held by the MySfxInstance, added to the sceneGraph, and therefore its ref count is 1).

Of course, in osgDotNet (managed code), ref_ptr and osg::Referenced::ref()/unref() aren't ported.
I'm assuming that reference count managing is done according CLR behaviour (as long as a reference on your instance is held by someone, the CLR doesn't invoke Dispose on that instance). 

Am I missing something obvious ?
Am I the only one using osgDotNet having this problem ?

{ Hereafter follow the same code I sent on friday, focusing on the important class and functions and simplified of #regions and unsignicant comments : }

>>>>> file MySfx.cs <<<<<

public class MySfx
{
  protected Osg.PositionAttitudeTransform _root;
  protected Osg.Group _psScene;

  public MySfx(float fFar)
  {
    _root = null;
    _psScene = null;

    // Sub scenegraph creation
    _root = new Osg.PositionAttitudeTransform();
    _root.setName("SkyModel_Root");
    //_root.setDataVariance(Osg.Object.DataVariance.DYNAMIC);

    // Init update callback
    _root.setUpdateCallback(new Oktal.OvOsg.MySfxUpdateCallback(this));
  }

  public void setScene(Osg.Group psScene)
  {
    if (psScene != null)
    {
        _psScene = psScene;
        _psScene.addChild(_root);
    }
  }

  public void update() 
  { 
    // Place update code here
  } 
}

internal class MySfxUpdateCallback: Osg.NodeCallback
{
   protected Oktal.OvOsg.MySfx _mySfx;

   public MySfxUpdateCallback(Oktal.OvOsg.MySfx mysfx) 
   {
     _mySfx = mysfx; 
   }

   public override void op_FunctionCall(Osg.Node node, Osg.NodeVisitor nv)
   {
       if (_mySfx != null)
       {
           _mySfx.update(); 
       }
       traverse(node, nv);
   }
}

>>>>> file Program.cs <<<<<

static void Main(string[] args)
{
    // (...)

    // load the data
    Osg.Node loadedModel = OsgViewerExe.OsgDotNetGlobals.osgDBReadNodeFiles(arguments, new OsgDB.ReaderWriter.Options());
    
     // (...)

     // optimize the scene graph, remove redundant nodes and state etc.
     OsgUtil.Optimizer optimizer = new OsgUtil.Optimizer();
     optimizer.optimize(loadedModel);

     // DEDICATED CODE HERE
     Osg.Group root = new Osg.Group();
     Osg.BoundingSphere info = loadedModel.computeBound();
     root.addChild(loadedModel);

     Oktal.OvOsg.MySfx sfx = new Oktal.OvOsg.MySfx(info.radius());
     sfx.setScene(root); // equals root.addChild(skyModel);
     // END DEDICATED CODE

     // Comment that (default view is away from the scene) shows the bug better...
     //viewer.getCameraManipulator().setHomePosition(new Osg.Vec3d(0.0, -10.0, 50.0), new Osg.Vec3d(0.0, 0.0, 50.0), new Osg.Vec3d(0.0, 0.0, 1.0)); 

     viewer.setSceneData(root);
     viewer.run();

}

>>>>>>>>>>>>>><<<<<<<<<<<<<<<<

Thx for any advice !

-- 
Christophe Médard
Société OKTAL (http://www.oktal.fr)
2 impasse Boudeville
31100 Toulouse (France)
Tél. : (+33) 5 62 11 50 10
Fax : (+33) 5 62 11 50 29



----- Original Message ----- 
From: Christophe Medard 
To: osg-users at lists.openscenegraph.org 
Sent: Friday, September 21, 2007 5:47 PM
Subject: osgDotNet : Nodes adding to scene graph outside main() function scope


Hi all, 

If I can still think straight (after such a long week), there seems to be a major problem for people that want to code special Effects in osgDotNet in C#  : 
If you want to add to your main scenegraph osg::Nodes that are held by (because created, updated and modified by) an SFX class, you quickly have memory corruption problems due to the fact that Dispose calls are done on those nodes whose reference count is greater than 0.  

This problem doesn't occur in a similar code written in C++ - using native osg dlls - in which a destructor is implementable for the SFX class, and osg::Referenced::ref() (resp. unref()) methods can be called when osg::Nodes are created (resp. inside SFX class' destructor).

I'm attaching a very short C# example illustrating that.
The application crashes rapidly, in Release or Debug. The shallow Debug trace is :

I must say to be complete that for instance I'm using version 2.0.0 of OSG and osgWrappers.

If there's is a way to avoid those Dispose calls on regular Nodes I'm interested to know !!
(My preceeding attemps of sfx implementation in C# where done roughly entirely in the main() function, which doesn't raise problems...)

-- 
Christophe Médard
Société OKTAL (http://www.oktal.fr)
2 impasse Boudeville
31100 Toulouse (France)
Tél. : (+33) 5 62 11 50 10
Fax : (+33) 5 62 11 50 29
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.openscenegraph.org/pipermail/osg-users-openscenegraph.org/attachments/20070925/fe6e193e/attachment-0003.htm>


More information about the osg-users mailing list