# Wrapping C++ std::shared\_ptr and similar smart pointers

**URL:** <https://discuss.ocaml.org/t/wrapping-c-std-shared-ptr-and-similar-smart-pointers/3582>\
**Category:** Learning\
**Tags:** ffi, c\
**Created:** [March 27, 2019, 9:24pm UTC](https://discuss.ocaml.org/t/wrapping-c-std-shared-ptr-and-similar-smart-pointers/3582 "2019-03-27T21:24:13Z")\
**Posts on this page:** 1\
**Showing post:** 1

<div class="post-metadata">

**Author:** ![manuhornung](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/manuhornung/32/1683_2.png) [@manuhornung](https://discuss.ocaml.org/u/manuhornung)\
**Post date:** [March 27, 2019, 9:24pm UTC](https://discuss.ocaml.org/t/wrapping-c-std-shared-ptr-and-similar-smart-pointers/3582/1 "2019-03-27T21:24:13Z")

</div>

Hello everyone!

I’m trying to create Reason/OCaml bindings for the Skia 2D graphics library. The library makes heavy use of smart pointers similar to `std::shared_ptr`s, but they called them `sk_sp`.

Now my first idea for wrapping these was using a regular pointer that points to a shared pointer. That would trigger the release of the memory behind the shared pointer as soon as the local variable containing the shared pointer goes out of scope though.

I found a solution that looked promising to me in [https://github.com/ygrek/scraps/blob/master/cxx\_wrapped.h](https://github.com/ygrek/scraps/blob/master/cxx_wrapped.h) but now I heard that reducing the refcount in the finalizer is also not a good idea. Unfortunately I don’t know why that is not a good idea and I also don’t have a better one.

Can anyone help me understand this better and point me towards a better approach?

---

_[View the full topic](https://discuss.ocaml.org/t/wrapping-c-std-shared-ptr-and-similar-smart-pointers/3582)._
