OpenSceneGraph Forum Forum Index OpenSceneGraph Forum
Official forum which mirrors the existent OSG mailing lists. Messages posted here are forwarded to the mailing list and vice versa.
 
   FAQFAQ    SearchSearch    MemberlistMemberlist    RulesRules    UsergroupsUsergroups    RegisterRegister 
 Mail2Forum SettingsMail2Forum Settings  ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 
   AlbumAlbum  OpenSceneGraph IRC ChatOpenSceneGraph IRC Chat   SmartFeedSmartFeed 

using ref_ptr in any case


 
Post new topic   Reply to topic    OpenSceneGraph Forum Forum Index -> General
View previous topic :: View next topic  
Author Message
Meldryt
User


Joined: 02 Mar 2016
Posts: 35

PostPosted: Mon Mar 20, 2017 1:41 pm    Post subject:
using ref_ptr in any case
Reply with quote

Hi.
I try to avoid possible memory leaks and currently replacing all pointers of osg object types with ref_ptr.

I read the osgproducer article and some other threads about these reference pointers, but i wonder if there is any advantage for using ref_ptr also in local scope as temporary references to existing pointers.

Code:
osg::ref_ptr<osgViewer::View> pView = dynamic_cast<osgViewer::View*>(&aa);
[/code]
Back to top
View user's profile Send private message
robertosfield
OSG Project Lead


Joined: 18 Mar 2009
Posts: 10893

PostPosted: Mon Mar 20, 2017 2:43 pm    Post subject:
using ref_ptr in any case
Reply with quote

Hi Sebastian,

Using ref_ptr<> in local scope to objects t that are owned by external
scope is a good practice but isn't always required.

If your application is multi-threaded then the external scope could
unref and objects that you are using, so if you don't have your own
reference to it it would go out of scope and you'd end up with a
dangling pointer in your local scope.

If your application is single threaded then it should be safe to just
use C pointers in local scope while the object is kept alive by the
external scope.

Robert.

On 20 March 2017 at 13:41, Sebastian Schmidt <> wrote:
Quote:
Hi.
I try to avoid possible memory leaks and currently replacing all pointers of osg object types with ref_ptr.

I read the osgproducer article and some other threads about these reference pointers, but i wonder if there is any advantage for using ref_ptr also in local scope as temporary references to existing pointers.


Code:
osg::ref_ptr<osgViewer::View> pView = dynamic_cast<osgViewer::View*>(&aa);

[/code]

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








------------------
Post generated by Mail2Forum
Back to top
View user's profile Send private message
Meldryt
User


Joined: 02 Mar 2016
Posts: 35

PostPosted: Mon Mar 20, 2017 3:14 pm    Post subject:
Re: using ref_ptr in any case
Reply with quote

robertosfield wrote:
Hi Sebastian,

Using ref_ptr<> in local scope to objects t that are owned by external
scope is a good practice but isn't always required.

If your application is multi-threaded then the external scope could
unref and objects that you are using, so if you don't have your own
reference to it it would go out of scope and you'd end up with a
dangling pointer in your local scope.

If your application is single threaded then it should be safe to just
use C pointers in local scope while the object is kept alive by the
external scope.

Robert.


Thanks. I would like to stick to multi threading.
Even if it doesnt make sense in local scope i have a consitent way of using ref_ptr everytime.
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    OpenSceneGraph Forum Forum Index -> General All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You cannot download files in this forum

Similar Topics
Topic Author Forum Replies Posted
No new posts ScalarsToColors returns a wrong color... hartwigw Submission 2 Sun Nov 20, 2016 4:40 pm View latest post
No new posts Why ref_ptr node can not release memo... JJLee General 4 Fri Jul 08, 2016 5:41 am View latest post
No new posts OT: Oracle loses API IP case with Google Chris Hanson General 0 Thu May 26, 2016 10:00 pm View latest post
No new posts Case sensitivity fixes for mingw cros... robertosfield Submission 2 Tue Jan 19, 2016 3:01 pm View latest post
No new posts Case sensitivity fixes for mingw cros... Alberto Luaces Submission 0 Tue Oct 20, 2015 2:09 pm View latest post


Board Security Anti Bot Question MOD - phpBB MOD against Spam Bots
Powered by phpBB © 2001, 2005 phpBB Group
Protected by Anti-Spam ACP