1

I've recently started using OpenSceneGraph. I'm wondering how it deals with memory management( if at all ). For example, we have the geometry class. The geometry class accepts arrays of vertices, colours, texture coordinates as you'd expect. Except, it expects these things as a pointer to an object:

void Geometry::setVertexArray(Array* array)
{
    if (array && array->getBinding()==osg::Array::BIND_UNDEFINED) array->setBinding(osg::Array::BIND_PER_VERTEX);

    _vertexArray = array;

    dirtyDisplayList();
    dirtyBound();

    if (_useVertexBufferObjects && array) addVertexBufferObjectIfRequired(array);
}

This is from the OpenSceneGraph source code. As you can see an Array* is passed to the method, which is expected to be allocated by the user. If that's the case, who deletes it? Obviously not OSG( and that makes sense ), because we can see that it blindly overwrites the last array pointer when a new one is assigned. This is fine, however now the user must make sure that the array they allocated outlives the geometry class. Also for every array the user gives this geometry object, they must keep a reference to this array.

This leaves us with an absurd amount of pointers that must be deleted only after the object is deleted. The result is I'm finding this very cumbersome to use. Am I missing something here? Why is the system designed like this?

Ben
  • 1,816
  • 1
  • 20
  • 29

1 Answers1

2
osg::ref_ptr<Array>             _vertexArray;

_vertexArray is defined in the header like this. This means when it is assigned to array in that method, the reference count will increase to 1. When the reference is removed the count will go to 0 and will delete the object.

This means you can't rely on the array data outside the geometry object unless you also defined the array as a ref_ptr. This is quite confusing and makes me wonder why they didn't have the parameter be a ref_ptr.

I'm sure this will lead to some obscure problems, but I guess the rule is do not rely on non-ref pointers if you're passing it on. This is probably meant for the cases where you blindly create an object and immediately pass it on to the method, though I wish OSG would make this more clear.

Ben
  • 1,816
  • 1
  • 20
  • 29
  • sorry can you please help me to install OpensceneGraph on Ubuntu.. i am struggling, don't know where to start and where it will end. – Irfan Ghaffar7 Mar 02 '15 at 14:24
  • 3
    Passing a ref_ptr would mean to copy an object, including the cost for extra memory allocation and object setup. Passing only the reference and wrapping right away with the ref_ptr seems a bit less costly. This as been like that for a loooong time and ppl are still using the engine; you'll get used to it :) – Vaillancourt Mar 03 '15 at 15:51