Please read Don's article of ref_ptr<>:
http://dburns.dhs.org/OSG/Articles/RefPointers/RefPointers.html
On 10/6/06, Oliver Kutter <[EMAIL PROTECTED]> wrote:
release() releases the object and returns the pointer to it without decrementing the reference count. I'd recommend you own up an editor and walk through the code.
The only time you should ever use release is when have code something like:
Node* createNode()
{
ref_ptr<Node> node = new Node;
return node.release();
}
As this prevents the ref_ptr<> from deleting the node object when the ref pointer goes out of scope.
You'll see this usage pattern very occasionally in the OSG. Its not normal usage.
In you code block you basically have a memory leak.
This in dangerous as you should consider what might happen if you addinged object2 into the scene graph - it'd take a reference to it, but then you've got an forceable deleted it.
Reference counting is design to prevent this problem, its a very common tehcnique now in computing so you should be able to find plenty of refences on the web about it.
Another trick is my you create your MyClass you shoudl make the destructor private/protected, this will prevent you from doing a delete object2 at compile time. Have a look at all the core OSG class that are derived from osg::Referenced, you'll find they all have protected destructors to prevent you from doing dangerous things like manually deleting them.
I strongly recommend you invest some time learning about the OSG's ref pointers from the available docs, and also look to the web, and then write your own examples and play. TIme invested now will help the rest of your computing career.
Robert.
1:
osg::ref_ptr<MyClass> object1 = new MyClass();
object1.release();
release() releases the object and returns the pointer to it without decrementing the reference count. I'd recommend you own up an editor and walk through the code.
The only time you should ever use release is when have code something like:
Node* createNode()
{
ref_ptr<Node> node = new Node;
return node.release();
}
As this prevents the ref_ptr<> from deleting the node object when the ref pointer goes out of scope.
You'll see this usage pattern very occasionally in the OSG. Its not normal usage.
In you code block you basically have a memory leak.
2:
MyClass* object2 = new MyClass();
delete object2;
This in dangerous as you should consider what might happen if you addinged object2 into the scene graph - it'd take a reference to it, but then you've got an forceable deleted it.
Reference counting is design to prevent this problem, its a very common tehcnique now in computing so you should be able to find plenty of refences on the web about it.
Another trick is my you create your MyClass you shoudl make the destructor private/protected, this will prevent you from doing a delete object2 at compile time. Have a look at all the core OSG class that are derived from osg::Referenced, you'll find they all have protected destructors to prevent you from doing dangerous things like manually deleting them.
I strongly recommend you invest some time learning about the OSG's ref pointers from the available docs, and also look to the web, and then write your own examples and play. TIme invested now will help the rest of your computing career.
Robert.
_______________________________________________ osg-users mailing list [email protected] http://openscenegraph.net/mailman/listinfo/osg-users http://www.openscenegraph.org/
