smart_ptrs.rst 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. Smart pointers
  2. ##############
  3. std::unique_ptr
  4. ===============
  5. Given a class ``Example`` with Python bindings, it's possible to return
  6. instances wrapped in C++11 unique pointers, like so
  7. .. code-block:: cpp
  8. std::unique_ptr<Example> create_example() { return std::unique_ptr<Example>(new Example()); }
  9. .. code-block:: cpp
  10. m.def("create_example", &create_example);
  11. In other words, there is nothing special that needs to be done. While returning
  12. unique pointers in this way is allowed, it is *illegal* to use them as function
  13. arguments. For instance, the following function signature cannot be processed
  14. by pybind11.
  15. .. code-block:: cpp
  16. void do_something_with_example(std::unique_ptr<Example> ex) { ... }
  17. The above signature would imply that Python needs to give up ownership of an
  18. object that is passed to this function, which is generally not possible (for
  19. instance, the object might be referenced elsewhere).
  20. std::shared_ptr
  21. ===============
  22. The binding generator for classes, :class:`class_`, can be passed a template
  23. type that denotes a special *holder* type that is used to manage references to
  24. the object. If no such holder type template argument is given, the default for
  25. a type named ``Type`` is ``std::unique_ptr<Type>``, which means that the object
  26. is deallocated when Python's reference count goes to zero.
  27. It is possible to switch to other types of reference counting wrappers or smart
  28. pointers, which is useful in codebases that rely on them. For instance, the
  29. following snippet causes ``std::shared_ptr`` to be used instead.
  30. .. code-block:: cpp
  31. py::class_<Example, std::shared_ptr<Example> /* <- holder type */> obj(m, "Example");
  32. Note that any particular class can only be associated with a single holder type.
  33. One potential stumbling block when using holder types is that they need to be
  34. applied consistently. Can you guess what's broken about the following binding
  35. code?
  36. .. code-block:: cpp
  37. class Child { };
  38. class Parent {
  39. public:
  40. Parent() : child(std::make_shared<Child>()) { }
  41. Child *get_child() { return child.get(); } /* Hint: ** DON'T DO THIS ** */
  42. private:
  43. std::shared_ptr<Child> child;
  44. };
  45. PYBIND11_MODULE(example, m) {
  46. py::class_<Child, std::shared_ptr<Child>>(m, "Child");
  47. py::class_<Parent, std::shared_ptr<Parent>>(m, "Parent")
  48. .def(py::init<>())
  49. .def("get_child", &Parent::get_child);
  50. }
  51. The following Python code will cause undefined behavior (and likely a
  52. segmentation fault).
  53. .. code-block:: python
  54. from example import Parent
  55. print(Parent().get_child())
  56. The problem is that ``Parent::get_child()`` returns a pointer to an instance of
  57. ``Child``, but the fact that this instance is already managed by
  58. ``std::shared_ptr<...>`` is lost when passing raw pointers. In this case,
  59. pybind11 will create a second independent ``std::shared_ptr<...>`` that also
  60. claims ownership of the pointer. In the end, the object will be freed **twice**
  61. since these shared pointers have no way of knowing about each other.
  62. There are two ways to resolve this issue:
  63. 1. For types that are managed by a smart pointer class, never use raw pointers
  64. in function arguments or return values. In other words: always consistently
  65. wrap pointers into their designated holder types (such as
  66. ``std::shared_ptr<...>``). In this case, the signature of ``get_child()``
  67. should be modified as follows:
  68. .. code-block:: cpp
  69. std::shared_ptr<Child> get_child() { return child; }
  70. 2. Adjust the definition of ``Child`` by specifying
  71. ``std::enable_shared_from_this<T>`` (see cppreference_ for details) as a
  72. base class. This adds a small bit of information to ``Child`` that allows
  73. pybind11 to realize that there is already an existing
  74. ``std::shared_ptr<...>`` and communicate with it. In this case, the
  75. declaration of ``Child`` should look as follows:
  76. .. _cppreference: http://en.cppreference.com/w/cpp/memory/enable_shared_from_this
  77. .. code-block:: cpp
  78. class Child : public std::enable_shared_from_this<Child> { };
  79. .. _smart_pointers:
  80. Custom smart pointers
  81. =====================
  82. pybind11 supports ``std::unique_ptr`` and ``std::shared_ptr`` right out of the
  83. box. For any other custom smart pointer, transparent conversions can be enabled
  84. using a macro invocation similar to the following. It must be declared at the
  85. top namespace level before any binding code:
  86. .. code-block:: cpp
  87. PYBIND11_DECLARE_HOLDER_TYPE(T, SmartPtr<T>);
  88. The first argument of :func:`PYBIND11_DECLARE_HOLDER_TYPE` should be a
  89. placeholder name that is used as a template parameter of the second argument.
  90. Thus, feel free to use any identifier, but use it consistently on both sides;
  91. also, don't use the name of a type that already exists in your codebase.
  92. The macro also accepts a third optional boolean parameter that is set to false
  93. by default. Specify
  94. .. code-block:: cpp
  95. PYBIND11_DECLARE_HOLDER_TYPE(T, SmartPtr<T>, true);
  96. if ``SmartPtr<T>`` can always be initialized from a ``T*`` pointer without the
  97. risk of inconsistencies (such as multiple independent ``SmartPtr`` instances
  98. believing that they are the sole owner of the ``T*`` pointer). A common
  99. situation where ``true`` should be passed is when the ``T`` instances use
  100. *intrusive* reference counting.
  101. Please take a look at the :ref:`macro_notes` before using this feature.
  102. By default, pybind11 assumes that your custom smart pointer has a standard
  103. interface, i.e. provides a ``.get()`` member function to access the underlying
  104. raw pointer. If this is not the case, pybind11's ``holder_helper`` must be
  105. specialized:
  106. .. code-block:: cpp
  107. // Always needed for custom holder types
  108. PYBIND11_DECLARE_HOLDER_TYPE(T, SmartPtr<T>);
  109. // Only needed if the type's `.get()` goes by another name
  110. namespace pybind11 { namespace detail {
  111. template <typename T>
  112. struct holder_helper<SmartPtr<T>> { // <-- specialization
  113. static const T *get(const SmartPtr<T> &p) { return p.getPointer(); }
  114. };
  115. }}
  116. The above specialization informs pybind11 that the custom ``SmartPtr`` class
  117. provides ``.get()`` functionality via ``.getPointer()``.
  118. .. seealso::
  119. The file :file:`tests/test_smart_ptr.cpp` contains a complete example
  120. that demonstrates how to work with custom reference-counting holder types
  121. in more detail.