[osg-users] Vec3Array instantiation

Sebastian Messerschmidt sebastian.messerschmidt at gmx.de
Thu Apr 7 01:33:02 PDT 2016


Hi Vincent,
> Hello
>
> I'm trying to use osg:Vec3Array with a c++11 smart pointer instead of osg smart pointer. I aim to join two APIs: osg and another one using C++11 smart pointer. Apparently, this is a destructor problem: ~TemplateArray<>() is private, so an explicit delete doesn't work. I have test many cases to understand the problem.
>
>
> Code:
> osg::Vec3Array * test = new osg::Vec3Array();
> delete test; // error here
>
>
>
>
> Code:
> std::shared_ptr<osg::Vec3Array> test2(new osg::Vec3Array()); // error due to pointer releasing
>
>
>
>
> Code:
> osg::Vec3Array test; // not work
>
>
>
>
> Code:
> osg::ref_ptr<osg::Vec3Array> test3 = new osg::Vec3Array(); // this work fine.
>
>
>
> So there are few solutions to use the Vec3Array: use osg smart pointer. What is the reason the destructor is protected even through the method is empty? Maybe to force developers to use smart pointers but I'm constrained to use c++11 smart pointer due to the second api. I think the only way, is to create a C++11 smart pointer templated by osg smart pointer or a class which contains the osg smart pointer.
First of all, the osg will manage the array fine when you keep it in a 
drawable, so there should no need to use your own management. If you 
need to hold it outside, simply use the ref_ptr. the ref_ptr is 
basically an smart-pointer for osg::Object derived objects.
The reason the destructor is private is to prevent stack-instances of 
the type and to disallow use patterns like yours.
If i remember correctly there was some guide on this in the wiki/trac.


Do you have an idea to use C+11 smart pointer instead of osg one?

Simply don't. It would not solve a single problem and would create 
pitfalls.
Use the osg::ref_ptr if you need to keep an object (it acts as a "shared 
ptr"). If you need a week smart ptr you can use observer pointer.


Cheers
Sebastian

>
> Thank you!
>
> Cheers,
> Vincent
>
> ------------------
> Read this topic online here:
> http://forum.openscenegraph.org/viewtopic.php?p=66760#66760
>
>
>
>
>
> _______________________________________________
> osg-users mailing list
> osg-users at lists.openscenegraph.org
> http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org




More information about the osg-users mailing list