Re: [osg-users] Why doesn't the viewer first destroy existing camera before assigning new camera?

2012-06-12 Thread Robert Osfield
Hi Joseph,

The OSG uses smart pointers (ref_ptr/observer_ptr) coupled with
reference counting to prevent memory leaks/dangling pointers.  So the
example you provide is prefectly safe, as soon as you assign the new
Camera to the internal ref_ptr the ref_ptr<> will call unref()
on the old Camera and ref() on the new Camera.

Lots has been written on ref_ptr<> usage over years on the mailing
list/forum and the books contain information on it as well so I would
recommend having look at these to understand about smart pointer
usage.

Robert.

On 15 May 2012 10:43, Nav Joseph  wrote:
> First post in this forum. While trying to understand the viewer, I was going 
> thru the OSG source code and saw that when a master camera is assigned to the 
> viewer thru setCamera, OSG does not actually destroy the existing default 
> master camera.
> See...
>
> Code:
> void View::setCamera(osg::Camera* camera)
> {
>    if (_camera.valid()) _camera->setView(0);
>
>    _camera = camera;
>
>    if (_camera.valid())
>    {
>        _camera->setView(this);
>        _camera->setRenderer(createRenderer(camera));
>    }
> }
>
>
>
> Why is it like this? Won't this result in a memory leak when I do:
> osg::ref_ptr camera = new osg::Camera;
> osgViewer::Viewer viewer;
> viewer.setCamera( camera.get() );
>
> Thank you!
>
> Cheers,
> Nav
>
> --
> Read this topic online here:
> http://forum.openscenegraph.org/viewtopic.php?p=47711#47711
>
>
>
>
>
> ___
> osg-users mailing list
> osg-users@lists.openscenegraph.org
> http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org
___
osg-users mailing list
osg-users@lists.openscenegraph.org
http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org


[osg-users] Why doesn't the viewer first destroy existing camera before assigning new camera?

2012-06-12 Thread Nav Joseph
First post in this forum. While trying to understand the viewer, I was going 
thru the OSG source code and saw that when a master camera is assigned to the 
viewer thru setCamera, OSG does not actually destroy the existing default 
master camera.
See...

Code:
void View::setCamera(osg::Camera* camera)
{
if (_camera.valid()) _camera->setView(0);

_camera = camera;

if (_camera.valid())
{
_camera->setView(this);
_camera->setRenderer(createRenderer(camera));
}
}



Why is it like this? Won't this result in a memory leak when I do: 
osg::ref_ptr camera = new osg::Camera;
osgViewer::Viewer viewer;
viewer.setCamera( camera.get() );

Thank you!

Cheers,
Nav

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





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