namal.bambarasinghe_ Posted January 11, 2018 Share Posted January 11, 2018 It looks like there is no way to cast the Unigine::Ptr to the base type. Do we have to release the ownership everytime to get a NodePtr? for example, Unigine::NodePtr createNewNode(const std::string& name) { auto dummy_node = Unigine::NodeDummy::create(name.c_str()); dummy_node->release(); return dummy_node->getNode(); } Is there a way to remove the, dummy_node->release(); function call? Thanks in advance Link to comment
alexander Posted January 15, 2018 Share Posted January 15, 2018 Hi, namal.bambarasinghe! Quote Is there a way to remove the, dummy_node->release(); function call? No.See what happens:1) You create a node inside a function scope (createNewNode).2) When a "return" is called, a dummy_node calls ~destructor(), which kills the node.3) Because object was destroyed, the result that function is always equals to NULL.The ->release() function resets the inner counter inside Ptr. After this, when ~destructor() is called, the object will not be deleted. Please, do not forget to call the ->grab() after calling the createNewNode()! Otherwise, the object will never be deleted automatically (only when you call .destroy()). Best regards, Alexander Link to comment
namal.bambarasinghe_ Posted January 18, 2018 Author Share Posted January 18, 2018 Hi Alexander, I am trying to achive the following - Unigine::NodePtr createNewNode(const std::string& name) { return Unigine::NodeDummy::create(name.c_str()); } The above is similar to std::shared_ptr behaviour as shown below, class Base { }; class Derived : public Base { }; std::shared_ptr<Base> createDerived() { return std::make_shared<Derived>(); } In the above code block, the smart pointer is automatically converted to the base type. Is there any reason why the Unigine::Ptr doesn't allow this type of behaviour (allow up casting)? Link to comment
alexander Posted January 18, 2018 Share Posted January 18, 2018 Hi, Namal! Quote Is there any reason why the Unigine::Ptr doesn't allow this type of behaviour (allow up casting)? Inside the engine you are manipulating not with the Node/NodeDummy, but with NodeInterface/NodeDummyInterface wrappers. And they are not inherited directly from each other. This is a bad design, unfortunately. A legacy that we can not remove yet. So, you should use ::cast() or ->getNode()/->getObject() methods to convert one type to another. And ->release() / ->grab() methods to resolve "smart" pointers corner cases. Best regards, Alexander Link to comment
Recommended Posts