Re: [osg-users] ref_ptr rules
Hi, ... Thanks. That makes sense.I will working on it!! Thank you! Cheers, paul 8)' -- Read this topic online here: http://forum.openscenegraph.org/viewtopic.php?p=37772#37772 ___ osg-users mailing list osg-users@lists.openscenegraph.org http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org
Re: [osg-users] ref_ptr rules
Old, but still relevant, write-up on ref_ptr: http://andesengineering.com/OSG_ProducerArticles/RefPointers/RefPointers.html http://andesengineering.com/OSG_ProducerArticles/RefPointers/RefPointers.html Glenn Waldron / Pelican Mapping / 703.652.4791 / @glennwaldron 2011/3/16 Sergey Polischuk pol...@yandex.ru Hi, Yasser osg ref_ptr's are intrusive, and object's reference count get's incremented every time you create ref_ptr to this object, and it decrease every time this ref_ptr destroyed. Inside osg all objects that keep pointers on other objects use ref_ptr's for that purpose, so if you pass simple pointer to some osg object it'll be refcounted correctly. You can not use delete on objects pointer that can be wrapped in ref_ptr because they have protected destructor and free when there are no more references to them. If you get pointer and need to be sure that it will be valid over time, you should create ref_ptr out of this pointer (thus increasing ref count) and keep it instead. In general if you create osg objects that can be wrapped into ref_ptr's you should make ref_ptr out of object's pointer at some point (though this can happen inside osg code if you pass pointer to some osg object which keeps it with ref_ptr) or they will not be destructed. Cheers, Sergey. 16.03.2011, 23:59, Yasser Asmi ya...@amazon.com: I am new to OSG and while I understand the general reference counting rules, I am having some trouble finding all the ref_ptr rules we should be following. For instance most examples in OSG do not use ref_ptr on objects and also do not free them. Sometime, these objects (Geometry, Vec3Array etc) are passed to methods. It is not clear if the methods are refcounting etc. Stepping through the source is a way to find out but I am looking for general guidance. Is there a resource that talks about it? Thank you! Cheers, Yasser -- Read this topic online here: http://forum.openscenegraph.org/viewtopic.php?p=37666#37666 ___ 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 mailing list osg-users@lists.openscenegraph.org http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org
Re: [osg-users] ref_ptr rules
Using ref_ptr everywhere is a good way to be exception safe because you know the pointer will be deleted even it it wasn't added to another OSG object. There is a possible pitfall if you want to return a ref_ptr in a function that returns a pointer, because the object will be deleted before the function returns. In that case, you must use the release() method of the ref_ptr object : osg::Node *createNode() { osg::ref_ptrosg::Node newNode = new osg::Group; // for instance // here you are exception safe return newNode.release(); // to avoid returning a pointer to a deleted object } osg::ref_ptrosg::Node myNode = createNode(); Regards, -Fred - Glenn Waldron gwald...@gmail.com a écrit : Old, but still relevant, write-up on ref_ptr: http://andesengineering.com/OSG_ProducerArticles/RefPointers/RefPointers.html Glenn Waldron / Pelican Mapping / 703.652.4791 / @glennwaldron 2011/3/16 Sergey Polischuk pol...@yandex.ru Hi, Yasser osg ref_ptr's are intrusive, and object's reference count get's incremented every time you create ref_ptr to this object, and it decrease every time this ref_ptr destroyed. Inside osg all objects that keep pointers on other objects use ref_ptr's for that purpose, so if you pass simple pointer to some osg object it'll be refcounted correctly. You can not use delete on objects pointer that can be wrapped in ref_ptr because they have protected destructor and free when there are no more references to them. If you get pointer and need to be sure that it will be valid over time, you should create ref_ptr out of this pointer (thus increasing ref count) and keep it instead. In general if you create osg objects that can be wrapped into ref_ptr's you should make ref_ptr out of object's pointer at some point (though this can happen inside osg code if you pass pointer to some osg object which keeps it with ref_ptr) or they will not be destructed. Cheers, Sergey. 16.03.2011, 23:59, Yasser Asmi ya...@amazon.com : I am new to OSG and while I understand the general reference counting rules, I am having some trouble finding all the ref_ptr rules we should be following. For instance most examples in OSG do not use ref_ptr on objects and also do not free them. Sometime, these objects (Geometry, Vec3Array etc) are passed to methods. It is not clear if the methods are refcounting etc. Stepping through the source is a way to find out but I am looking for general guidance. Is there a resource that talks about it? Thank you! Cheers, Yasser -- Read this topic online here: http://forum.openscenegraph.org/viewtopic.php?p=37666#37666 ___ 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 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] ref_ptr rules
I am new to OSG and while I understand the general reference counting rules, I am having some trouble finding all the ref_ptr rules we should be following. For instance most examples in OSG do not use ref_ptr on objects and also do not free them. Sometime, these objects (Geometry, Vec3Array etc) are passed to methods. It is not clear if the methods are refcounting etc. Stepping through the source is a way to find out but I am looking for general guidance. Is there a resource that talks about it? Thank you! Cheers, Yasser -- Read this topic online here: http://forum.openscenegraph.org/viewtopic.php?p=37666#37666 ___ osg-users mailing list osg-users@lists.openscenegraph.org http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org
Re: [osg-users] ref_ptr rules
Hi, Yasser osg ref_ptr's are intrusive, and object's reference count get's incremented every time you create ref_ptr to this object, and it decrease every time this ref_ptr destroyed. Inside osg all objects that keep pointers on other objects use ref_ptr's for that purpose, so if you pass simple pointer to some osg object it'll be refcounted correctly. You can not use delete on objects pointer that can be wrapped in ref_ptr because they have protected destructor and free when there are no more references to them. If you get pointer and need to be sure that it will be valid over time, you should create ref_ptr out of this pointer (thus increasing ref count) and keep it instead. In general if you create osg objects that can be wrapped into ref_ptr's you should make ref_ptr out of object's pointer at some point (though this can happen inside osg code if you pass pointer to some osg object which keeps it with ref_ptr) or they will not be destructed. Cheers, Sergey. 16.03.2011, 23:59, Yasser Asmi ya...@amazon.com: I am new to OSG and while I understand the general reference counting rules, I am having some trouble finding all the ref_ptr rules we should be following. For instance most examples in OSG do not use ref_ptr on objects and also do not free them. Sometime, these objects (Geometry, Vec3Array etc) are passed to methods. It is not clear if the methods are refcounting etc. Stepping through the source is a way to find out but I am looking for general guidance. Is there a resource that talks about it? Thank you! Cheers, Yasser -- Read this topic online here: http://forum.openscenegraph.org/viewtopic.php?p=37666#37666 ___ 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
Re: [osg-users] ref_ptr rules
Thanks. That makes sense but I still have a question. If I am createing a new OSG object in my code, say osg:Geometry, and I know it will be passed to a osg method at some point. How do I know if I should or should not wrap it in ref_ptr? I'd think we should wrap it. But I have seen a lot of examples that do not. I am concerned about memory leaks. Cheers, Yasser -- Read this topic online here: http://forum.openscenegraph.org/viewtopic.php?p=37668#37668 ___ osg-users mailing list osg-users@lists.openscenegraph.org http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org
Re: [osg-users] ref_ptr rules
On 17/03/11 11:33 , Yasser Asmi wrote: Thanks. That makes sense but I still have a question. If I am createing a new OSG object in my code, say osg:Geometry, and I know it will be passed to a osg method at some point. How do I know if I should or should not wrap it in ref_ptr? I'd think we should wrap it. But I have seen a lot of examples that do not. I am concerned about memory leaks. Most of the examples create OSG objects, pass them on to another OSG object (where it will be reference counted) and do not care about them afterwards. If you keep a reference to the object (as well as pass it on to another OSG object) then you must use a ref_ptr, otherwise it can be deleted and you end up with a dangling object. This is okay: void createStuff(osg::Group* group) { // This is okay, the drawable will be owned by the group osg::Geometry* drawable = new osg::Geometry(); group-addChild(drawable); } But this is not: class Stuff { osg::Geometry* _drawable; void createStuff(osg::Group* group) { // Bad things waiting to happen... _drawable = new osg::Geometry(); group-addChild(_drawable); } } Cheers, /ulrich ___ osg-users mailing list osg-users@lists.openscenegraph.org http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org