Is there a C++/CLI RAII smart pointer class for containment of a native pointer in a managed type? Just wondering, before I go write my own clr_scoped_ptr
value class template.
I'm aware of the Microsoft-provided:
containment of a managed handle in a native class: auto_gcroot
containment of a managed handle in a managed class: auto_handle
The above two are similar to auto_ptr
or unique_ptr
.
counted_handle
here, similar to shared_ptr
But all these are for disposing managed ref class instances, not for freeing native objects.
C++11 comes up with its own mechanism that's Smart Pointer. When the object is destroyed it frees the memory as well. So, we don't need to delete it as Smart Pointer does will handle it. A Smart Pointer is a wrapper class over a pointer with an operator like * and -> overloaded.
The scoped_ptr class template stores a pointer to a dynamically allocated object. (Dynamically allocated objects are allocated with the C++ new expression.) The object pointed to is guaranteed to be deleted, either on destruction of the scoped_ptr, or via an explicit reset.
Concept of the C++11 Smart Pointers Smart pointers are class objects that behave like built-in pointers but also manage objects that you create with new so that you don't have to worry about when and whether to delete them - the smart pointers automatically delete the managed object for you at the appropriate time.
This one looks fairly complete, but I'm not looking for silent transfer of ownership ala auto_ptr
.
I've posted my version under a rather permissive license over at codereview.se
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With