Jump to content

Owned vs non-owned pointers


photo

Recommended Posts

Hello,

Had this code (that was leaking memory):

Unigine::PropertyPtr property;

...

Unigine::PropertyParameterPtr paramRoot = property->getParameterPtr();
const int propertyParamsNo = paramRoot->getNumChildren();

for(int I=0; I<propertyParamsNo; ++I)
{        
   auto param = paramRoot->getChild(I);
   ...
}

At the above, param, which is PropertyParameterPtr is not an owned pointer even if allocated at each getChild call. Why is that?

Basically I have to call param->setOwner(true) at each getChild call, so when it goes out of scope its underlying raw pointer is deleted. This is a very unsafe way to work with smart pointers. Maybe I misunderstood some details, but I thought that non owned pointers are the ones that have data always maintained by the engine internally.

Regards,
Adrian

 

Link to comment

Hi adrian.licuriceanu,

Why is that?
Yeah, this is a bug. We already fixed it in SDK 2.15 (or 2.15.1, I don't remember, sorry).

Best regards,
Alexander

  • Like 1
Link to comment

Hello, so for now, as an workaround since we are still on Unigine 2.13.0.1, I have setOwner(true) called after each get. But I was wondering if it is better to just cache the pointers too (not just call get each time i need them in the loops)? I know you are using some pooling, but still allocate / deallocate in each block seems expensive. What is your suggestion?

Regards,
Adrian

Link to comment

But I was wondering if it is better to just cache the pointers too (not just call get each time i need them in the loops)?
Yes, sure. If you can cache pointers, cache them. But don't forget to set setOwner(true) for each new pointer.

  • Like 1
Link to comment
×
×
  • Create New...