Closed neshume closed 7 years ago
Usually passing a const-ref (const Ptr<X>&
) is the right thing, since this doesn't involve any ref-count bumping or validity checks.
...but having said that... if you prefer the std smart pointers (shared_ptr or unique_ptr), for your own classes, you can do this as well and it may be more flexible, just don't mix them with Oryol's RefCounted class and Ptr<>. Oryol only uses its Ptr class in very few places, and I intend to keep its usage in 'public APIs' as minimal as possible.
Hi F,
I understand ... and I have no intention of mixing the shared_ptr crap into the code.. but I had some very bad crashes regarding oryol::ptr handling and validating pointers ... so I could keep regular pointers passing to functions ... but I think it could be dangerous...
Can you describe what type of crashes you are getting? There are a few typical errors with C++ references in general, since under the hood they are just memory addresses.
For instance if the reference points into a dynamic array, you'll have to be aware of 'iterator invalidation': if the array grows while the reference exists, the underlying storage of the array will move somewhere else in memory, and the reference will point to memory that no longer has valid content.
Or if you erase elements from the array while a reference to an item in the array exists, the reference may point to a different item or past the end of the array.
The types you described are of course memory management 101 .. i am talking about the exotic stuff ..like
allocation on the stack of a Oryol::ptr
cheers,
neshume
Hi Floooh,
The issue was found .. i was partially conencted to the Oryol::PTR stuff ;o)
It seems resources were not released (too many refs hanging around) causing ResourcePool to exceed the 128 limit (current) of the number of pool resources.. causing an assert (even a justified one) ..
So .. I will minimize the usage of the Oryol::Ptr and handle the memory traditional way ;o)
Cheers,
Neshume
Is it wise to keep Oryol::Ptr<> when using functions or use const ref ? or maybe even use the bare internal pointer..
std::smart_ptr has many forms .. Oryol::Ptr<> is a little different what is the preferred strategy when moving the smart pointer around?
cheers,