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 

osg::observer_ptr and osg::ref_ptr


 
Post new topic   Reply to topic    OpenSceneGraph Forum Forum Index -> General
View previous topic :: View next topic  
Author Message
Cysneros, Nelson A CIV...
Guest





PostPosted: Fri Jul 10, 2009 10:46 pm    Post subject:
osg::observer_ptr and osg::ref_ptr
Reply with quote

So sorry if this is a way too obvious question but what is the difference between observer_ptr and ref_ptr? And when would you use one over the other?


osg::observer_ptr : Smart pointer for observed objects, that automatically set pointers to them to null when they deleted
osg::ref_ptr : Smart pointer for handling referenced counted objects


They seem to do the same thing. At least I was able to replace all observer_ptr with ref_ptr and the example still compiled and ran.


Best Regards

------------------
Post generated by Mail2Forum
Back to top
Jason Daly
Guest





PostPosted: Sat Jul 11, 2009 12:30 am    Post subject:
osg::observer_ptr and osg::ref_ptr
Reply with quote

Cysneros, Nelson A CIV SPAWAR SSC PAC, 56510 wrote:
Quote:
Message So sorry if this is a way too obvious question but what is the difference between observer_ptr and ref_ptr? And when would you use one over the other?


osg::observer_ptr : Smart pointer for observed objects, that automatically set pointers to them to null when they deleted
osg::ref_ptr : Smart pointer for handling referenced counted objects


They seem to do the same thing. At least I was able to replace all observer_ptr with ref_ptr and the example still compiled and ran.

They're quite different, actually. When a ref_ptr is assigned the address of an object, it increases the reference count on the object. Likewise, when it is detached from the object (either by deleting the ref_ptr, letting it go out of scope, or assigning it a different address), the reference count on the first object is decreased. Whenever a Referenced object's reference count drops to zero, the object is deleted.

observer_ptr never changes the reference count of the object to which it points. The main benefit of an observer_ptr is that when the pointed object is deleted, the value of the observer_ptr is automatically set to NULL.


For example:


observer_ptr<Node> nodeObs;
ref_ptr<Node> nodeRef;


nodeRef = new Node(); // Node's ref count is now one
nodeObs = nodeRef.get(); // Node's ref count is still one

nodePtr = NULL; // Node's ref count is now zero, and is deleted
// nodeObs is now NULL


OK, not a great example, but hopefully you get the idea Wink

--"J"

------------------
Post generated by Mail2Forum
Back to top
krikr42
Newbie


Joined: 03 May 2018
Posts: 11

PostPosted: Thu Oct 18, 2018 1:20 pm    Post subject:
Reply with quote

Seems handy!

So how can nodeObs become NULL when the Node object is deleted? Does the latter store a list of the observer_ptr objects that it is observed by, and set them all to NULL when it is destructed? Or how does it work?

Cheers,
Kristofer
Back to top
View user's profile Send private message
robertosfield
OSG Project Lead


Joined: 18 Mar 2009
Posts: 12149

PostPosted: Thu Oct 18, 2018 7:26 pm    Post subject:
osg::observer_ptr and osg::ref_ptr
Reply with quote

HI Kristofer,

There is an intermediate ObserveSet object that both the object being observed and the observer_ptr<> share, the the observed gets deleted it resets the ObserverSet so that when then the observer_ptr<> is accessed it can check to see if the pointer is still valid.


The ObserverSet is created on demand behind the scenes, you don't need to worry it's management, just use observer_ptr<> and things will be set up automatically for you Smile


Robert,


On Thu, 18 Oct 2018 at 18:00, Kristofer Krus < (
Only registered users can see emails on this board!
Get registred or enter the forums!
)> wrote:

Quote:
Seems handy!

So how can nodeObs become NULL when the Node object is deleted? Does the latter store a list of the observer_ptr objects that it is observed by, and set them all to NULL when it is destructed? Or how does it work?

Cheers,
Kristofer

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





_______________________________________________
osg-users mailing list
(
Only registered users can see emails on this board!
Get registred or enter the forums!
)
http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org


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


Joined: 03 May 2018
Posts: 11

PostPosted: Tue Oct 23, 2018 11:54 am    Post subject:
Re: osg::observer_ptr and osg::ref_ptr
Reply with quote

Hi Robert,

Ah, that's clever! So the observer_ptr basically acts like ref_ptr when it comes to the ObserverSet object and deletes that object when the last observer_ptr object is destructed? Very Happy

Cheers!

Kristofer
Back to top
View user's profile Send private message
robertosfield
OSG Project Lead


Joined: 18 Mar 2009
Posts: 12149

PostPosted: Tue Oct 23, 2018 1:25 pm    Post subject:
osg::observer_ptr and osg::ref_ptr
Reply with quote

Hi Kristofer,

On Tue, 23 Oct 2018 at 13:30, Kristofer Krus < (
Only registered users can see emails on this board!
Get registred or enter the forums!
)> wrote:

Quote:
Ah, that's clever. So the observer_ptr basically acts like ref_ptr when it comes to the ObserverSet object and deletes that object when the last observer_ptr object is destructed? Very Happy


Yep, that's the idea.  The ObseverSet sticks around till the last Referenced object or the last observer_ptr/Observer unref's.



Robert.

 

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


Joined: 03 May 2018
Posts: 11

PostPosted: Wed Oct 24, 2018 12:59 pm    Post subject:
Reply with quote

Hi Robert,

Thanks. I'm having the problem that an observer_ptr that is assigned a pointer to an object that hasn't yet been assigned to a ref_ptr looks like it is invalid when I try to lock it (but not when I call the get method, so there is a discrepancy between observer_ptr::lock and observer_ptr::get in that sense). When the pointer is also assigned to a ref_ptr so that the reference count in the referenced object goes up, the lock method starts to return true instead of false.

Here is what I do: In the constructor of a class that is referencable, I create an observer_ptr, obs, that is assigned the this-pointer—pointing at object A, which is the object being constructed—and I send obs to another object B that wants to observe A. at this point, it looks like obs is invalid. Directly after A has been constructed—by using the new-keyword—its memory address is assigned to a ref_ptr and obs.lock suddenly starts to succeed.

Is this desirable behavior? I mean, the object exists, right? Why would otherwise get() succeed? Also, if you create an obs_ptr to an object located on the heap, that won't work as expected either when you try to lock it.

Cheers,
Kristofer
Back to top
View user's profile Send private message
gwaldron (Glenn Waldron)
Appreciator


Joined: 25 Aug 2011
Posts: 234

PostPosted: Wed Oct 31, 2018 12:16 pm    Post subject:
osg::observer_ptr and osg::ref_ptr
Reply with quote

Kristofer,

Yes. The semantics of observer_ptr guarantee the existence of the target object ONLY after calling lock(). Only by calling lock() can you know for certain that the target object exists; calling get() is insufficient because get() will return a raw pointer that may or may not actually still point to an existing object. Avoid observer_ptr::get() and always use the lock() idiom.


lock() populates a true ref_ptr with the object pointer. If there were no pre-existing references to the object, that new ref_ptr would destroy the object when it went out of scope -- that would be unexpected and undesirable behavior. So returning false makes sense.


I suspect that having a class creating an observer to itself is a design flaw Smile
Glenn Waldron / osgEarth






On Wed, Oct 24, 2018 at 8:59 AM Kristofer Krus < (
Only registered users can see emails on this board!
Get registred or enter the forums!
)> wrote:

Quote:
Hi Robert,

Thanks. I'm having the problem that an observer_ptr that is assigned a pointer to an object that hasn't yet been assigned to a ref_ptr looks like it is invalid when I try to lock it (but not when I call the get method, so there is a discrepancy between observer_ptr::lock and observer_ptr::get in that sense). When the pointer is also assigned to a ref_ptr so that the reference count in the referenced object goes up, the lock method starts to return true instead of false.

Here is what I do: In the constructor of a class that is referencable, I create an observer_ptr, obs, that is assigned the this-pointer—pointing at object A, which is the object being constructed—and I send obs to another object B that wants to observe A. at this point, it looks like obs is invalid. Directly after A has been constructed—by using the new-keyword—its memory address is assigned to a ref_ptr and obs.lock suddenly starts to succeed.

Is this desirable behavior? I mean, the object exists, right? Why would otherwise get() succeed? Also, if you create an obs_ptr to an object located on the heap, that won't work as expected either when you try to lock it.

Cheers,
Kristofer

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





_______________________________________________
osg-users mailing list
(
Only registered users can see emails on this board!
Get registred or enter the forums!
)
http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org


------------------
Post generated by Mail2Forum
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 osg::observer_ptr ldj General 0 Wed Sep 05, 2018 6:12 am View latest post
No new posts Potential bug in ref_ptr::assign hartwigw General 3 Tue Oct 24, 2017 10:34 pm View latest post
No new posts using ref_ptr in any case Meldryt General 2 Mon Mar 20, 2017 1:41 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 GraphicsWindowWin32 compiling errors ... Davide Raccagni Submission 4 Thu May 19, 2016 11:32 am 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