[osg-users] basic question on OSG smart pointers

2010-06-04 Thread Gianni Ambrosio

Hi All,
I have a simple and basic question about how OSG manage smart pointers 
and so on.


Is it true that if I declare a pointer as follows:

osg::Group* test = new osg::Group;

I would have a memory leak since I can not call a delete on test 
object pointer?


So, this is the reason why I should use a ref_ptr instead as follows?

osg::ref_ptrosg::Group test = new osg::Group;

In this case the smart pointer will take care of the object deletion as 
soon as the test variable goes out of scope.


Regards,
Gianni


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


Re: [osg-users] basic question on OSG smart pointers

2010-06-04 Thread Serge Lages
Hi,

Yes you're right, you can have more info here :

http://andesengineering.com/OSG_ProducerArticles/RefPointers/RefPointers.html

http://andesengineering.com/OSG_ProducerArticles/RefPointers/RefPointers.html
Cheers,

On Fri, Jun 4, 2010 at 6:18 PM, Gianni Ambrosio 
gianni.ambro...@vi-grade.com wrote:

 Hi All,
 I have a simple and basic question about how OSG manage smart pointers and
 so on.

 Is it true that if I declare a pointer as follows:

 osg::Group* test = new osg::Group;

 I would have a memory leak since I can not call a delete on test object
 pointer?

 So, this is the reason why I should use a ref_ptr instead as follows?

 osg::ref_ptrosg::Group test = new osg::Group;

 In this case the smart pointer will take care of the object deletion as
 soon as the test variable goes out of scope.

 Regards,
 Gianni


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




-- 
Serge Lages
http://www.tharsis-software.com
___
osg-users mailing list
osg-users@lists.openscenegraph.org
http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org


Re: [osg-users] basic question on OSG smart pointers

2010-06-04 Thread Jean-Sébastien Guay

Hi Gianni,

I'll add this to Serge's answer:


Is it true that if I declare a pointer as follows:

osg::Group* test = new osg::Group;

I would have a memory leak since I can not call a delete on test
object pointer?


You would have a leak if you never add test to a container that holds 
ref_ptrs. For example, if you add test as child to another group, or 
set it as scene data on your viewer, it will be contained in a ref_ptr 
and will be deleted when the last containing object goes out of scope.


The reason is that Referenced (the base class for ref counted objects, 
which Group inherits from indirectly) starts its reference count at 0, 
and every time you give the pointer to your object to a ref_ptr, it 
increments it. So for example:


main()
{
  osg::Group* group = new osg::Group;// ref count 0

  osgViewer::Viewer viewer;
  viewer.setSceneData(group);// ref count 1
}// ref count 0 -- delete

There will be no leak there, because at the end of the scope of main(), 
viewer will be destroyed, which destroys its member variables, one of 
which is the scene data, which is a ref_ptr. The ref count will fall to 
0 and the object to which group pointed will be deleted.


But having said that, it's never wrong to put it in a ref_ptr in the 
first place, and it can be wrong not to (it can lead to a leak if some 
things don't happen later, or it can lead to an already-deleted object 
being used if some other ref_ptr deleted it) so it's easier to always 
use ref_ptrs.


Hope that clears things up,

J-S
--
__
Jean-Sebastien Guayjean-sebastien.g...@cm-labs.com
   http://www.cm-labs.com/
http://whitestar02.webhop.org/
___
osg-users mailing list
osg-users@lists.openscenegraph.org
http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org


Re: [osg-users] basic question on OSG smart pointers

2010-06-04 Thread Jim Brooks
Yes, you're right.

A good OSG coding habit is to just always assign to osg::ref_ptr.


Is it true that if I declare a pointer as follows:

osg::Group* test = new osg::Group;

I would have a memory leak since I can not call a delete on test
object pointer?

So, this is the reason why I should use a ref_ptr instead as follows?

osg::ref_ptrosg::Group test = new osg::Group;

In this case the smart pointer will take care of the object deletion as
soon as the test variable goes out of scope.
___
osg-users mailing list
osg-users@lists.openscenegraph.org
http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org


Re: [osg-users] basic question on OSG smart pointers

2010-06-04 Thread Ulrich Hertlein
On 5/06/10 2:18 , Gianni Ambrosio wrote:
 Is it true that if I declare a pointer as follows:
 
 osg::Group* test = new osg::Group;
 
 I would have a memory leak since I can not call a delete on test
 object pointer?

As already said, yes you could have a memory leak.
What could also happen is that if you assign this to a ref_ptr (eg, by using 
addChild,
setSceneData, etc) *but* keep the raw pointer around as well you could end up 
with an
invalid pointer when the object was deleted by unreferencing.

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