improved shared pointer support (fixes #14)

This commit is contained in:
Wenzel Jakob 2015-11-24 23:05:58 +01:00
parent 5e90fa4e6a
commit 6e213c9ca0
6 changed files with 240 additions and 137 deletions

View File

@ -457,6 +457,14 @@ be declared at the top level before any binding code:
demonstrates how to work with custom reference-counting holder types in
more detail.
.. warning::
To ensure correct reference counting among Python and C++, the use of
``std::shared_ptr<T>`` as a holder type requires that ``T`` inherits from
``std::enable_shared_from_this<T>`` (see cppreference_ for details).
.. _cppreference: http://en.cppreference.com/w/cpp/memory/enable_shared_from_this
.. _custom_constructors:
Custom constructors

View File

@ -31,14 +31,36 @@ private:
int value;
};
class MyObject2 : public std::enable_shared_from_this<MyObject2> {
public:
MyObject2(int value) : value(value) {
std::cout << toString() << " constructor" << std::endl;
}
std::string toString() const {
return "MyObject2[" + std::to_string(value) + "]";
}
virtual ~MyObject2() {
std::cout << toString() << " destructor" << std::endl;
}
private:
int value;
};
/// Make pybind aware of the ref-counted wrapper type
PYBIND11_DECLARE_HOLDER_TYPE(T, ref<T>);
PYBIND11_DECLARE_HOLDER_TYPE(T, std::shared_ptr<T>);
Object *make_object_1() { return new MyObject(1); }
ref<Object> make_object_2() { return new MyObject(2); }
MyObject *make_myobject_4() { return new MyObject(4); }
ref<MyObject> make_myobject_5() { return new MyObject(5); }
MyObject2 *make_myobject2_1() { return new MyObject2(1); }
std::shared_ptr<MyObject2> make_myobject2_2() { return std::make_shared<MyObject2>(2); }
void print_object_1(const Object *obj) { std::cout << obj->toString() << std::endl; }
void print_object_2(ref<Object> obj) { std::cout << obj->toString() << std::endl; }
void print_object_3(const ref<Object> &obj) { std::cout << obj->toString() << std::endl; }
@ -49,6 +71,11 @@ void print_myobject_2(ref<MyObject> obj) { std::cout << obj->toString() << std::
void print_myobject_3(const ref<MyObject> &obj) { std::cout << obj->toString() << std::endl; }
void print_myobject_4(const ref<MyObject> *obj) { std::cout << (*obj)->toString() << std::endl; }
void print_myobject2_1(const MyObject2 *obj) { std::cout << obj->toString() << std::endl; }
void print_myobject2_2(std::shared_ptr<MyObject2> obj) { std::cout << obj->toString() << std::endl; }
void print_myobject2_3(const std::shared_ptr<MyObject2> &obj) { std::cout << obj->toString() << std::endl; }
void print_myobject2_4(const std::shared_ptr<MyObject2> *obj) { std::cout << (*obj)->toString() << std::endl; }
void init_ex8(py::module &m) {
py::class_<Object, ref<Object>> obj(m, "Object");
obj.def("getRefCount", &Object::getRefCount);
@ -69,5 +96,14 @@ void init_ex8(py::module &m) {
m.def("print_myobject_3", &print_myobject_3);
m.def("print_myobject_4", &print_myobject_4);
py::class_<MyObject2, std::shared_ptr<MyObject2>>(m, "MyObject2")
.def(py::init<int>());
m.def("make_myobject2_1", &make_myobject2_1);
m.def("make_myobject2_2", &make_myobject2_2);
m.def("print_myobject2_1", &print_myobject2_1);
m.def("print_myobject2_2", &print_myobject2_2);
m.def("print_myobject2_3", &print_myobject2_3);
m.def("print_myobject2_4", &print_myobject2_4);
py::implicitly_convertible<py::int_, MyObject>();
}

View File

@ -8,6 +8,8 @@ from example import make_object_1
from example import make_object_2
from example import make_myobject_4
from example import make_myobject_5
from example import make_myobject2_1
from example import make_myobject2_2
from example import print_object_1
from example import print_object_2
from example import print_object_3
@ -16,6 +18,10 @@ from example import print_myobject_1
from example import print_myobject_2
from example import print_myobject_3
from example import print_myobject_4
from example import print_myobject2_1
from example import print_myobject2_2
from example import print_myobject2_3
from example import print_myobject2_4
for o in [make_object_1(), make_object_2(), MyObject(3)]:
print("Reference count = %i" % o.getRefCount())
@ -35,3 +41,11 @@ for o in [make_myobject_4(), make_myobject_5(), MyObject(6), 7]:
print_myobject_2(o)
print_myobject_3(o)
print_myobject_4(o)
for o in [make_myobject2_1(), make_myobject2_2()]:
print(o)
print_myobject2_1(o)
print_myobject2_2(o)
print_myobject2_3(o)
print_myobject2_4(o)

View File

@ -1,261 +1,275 @@
MyObject[1] constructor
Initialized ref from pointer 0x7ff7d9742750
Initialized ref from pointer 0x7ffdb2c16a50
MyObject[2] constructor
Initialized ref from pointer 0x7ff7d9742150
Initialized ref from pointer 0x7ff7d9742150
Destructing ref 0x7ff7d9742150
Initialized ref from pointer 0x7ffdb2c167a0
Initialized ref from pointer 0x7ffdb2c167a0
Destructing ref 0x7ffdb2c167a0
MyObject[3] constructor
Initialized ref from pointer 0x7ff7d9741800
Initialized ref from pointer 0x7ffdb2c17420
Reference count = 1
MyObject[1]
Created empty ref
Initialized ref from pointer 0x7ff7d9742750
Move-assigning ref 0x7ff7d9742750
Initialized ref from pointer 0x7ffdb2c16a50
Move-assigning ref 0x7ffdb2c16a50
Destructing ref 0x0
Initialized ref from ref 0x7ff7d9742750
Initialized ref from ref 0x7ffdb2c16a50
MyObject[1]
Destructing ref 0x7ff7d9742750
Destructing ref 0x7ff7d9742750
Destructing ref 0x7ffdb2c16a50
Destructing ref 0x7ffdb2c16a50
Created empty ref
Initialized ref from pointer 0x7ff7d9742750
Move-assigning ref 0x7ff7d9742750
Initialized ref from pointer 0x7ffdb2c16a50
Move-assigning ref 0x7ffdb2c16a50
Destructing ref 0x0
MyObject[1]
Destructing ref 0x7ff7d9742750
Destructing ref 0x7ffdb2c16a50
Created empty ref
Initialized ref from pointer 0x7ff7d9742750
Move-assigning ref 0x7ff7d9742750
Initialized ref from pointer 0x7ffdb2c16a50
Move-assigning ref 0x7ffdb2c16a50
Destructing ref 0x0
MyObject[1]
Destructing ref 0x7ff7d9742750
Destructing ref 0x7ffdb2c16a50
Reference count = 1
MyObject[2]
Created empty ref
Initialized ref from pointer 0x7ff7d9742150
Move-assigning ref 0x7ff7d9742150
Initialized ref from pointer 0x7ffdb2c167a0
Move-assigning ref 0x7ffdb2c167a0
Destructing ref 0x0
Initialized ref from ref 0x7ff7d9742150
Initialized ref from ref 0x7ffdb2c167a0
MyObject[2]
Destructing ref 0x7ff7d9742150
Destructing ref 0x7ff7d9742150
Destructing ref 0x7ffdb2c167a0
Destructing ref 0x7ffdb2c167a0
Created empty ref
Initialized ref from pointer 0x7ff7d9742150
Move-assigning ref 0x7ff7d9742150
Initialized ref from pointer 0x7ffdb2c167a0
Move-assigning ref 0x7ffdb2c167a0
Destructing ref 0x0
MyObject[2]
Destructing ref 0x7ff7d9742150
Destructing ref 0x7ffdb2c167a0
Created empty ref
Initialized ref from pointer 0x7ff7d9742150
Move-assigning ref 0x7ff7d9742150
Initialized ref from pointer 0x7ffdb2c167a0
Move-assigning ref 0x7ffdb2c167a0
Destructing ref 0x0
MyObject[2]
Destructing ref 0x7ff7d9742150
Destructing ref 0x7ffdb2c167a0
Reference count = 1
MyObject[3]
Created empty ref
Initialized ref from pointer 0x7ff7d9741800
Move-assigning ref 0x7ff7d9741800
Initialized ref from pointer 0x7ffdb2c17420
Move-assigning ref 0x7ffdb2c17420
Destructing ref 0x0
Initialized ref from ref 0x7ff7d9741800
Initialized ref from ref 0x7ffdb2c17420
MyObject[3]
Destructing ref 0x7ff7d9741800
Destructing ref 0x7ff7d9741800
Destructing ref 0x7ffdb2c17420
Destructing ref 0x7ffdb2c17420
Created empty ref
Initialized ref from pointer 0x7ff7d9741800
Move-assigning ref 0x7ff7d9741800
Initialized ref from pointer 0x7ffdb2c17420
Move-assigning ref 0x7ffdb2c17420
Destructing ref 0x0
MyObject[3]
Destructing ref 0x7ff7d9741800
Destructing ref 0x7ffdb2c17420
Created empty ref
Initialized ref from pointer 0x7ff7d9741800
Move-assigning ref 0x7ff7d9741800
Initialized ref from pointer 0x7ffdb2c17420
Move-assigning ref 0x7ffdb2c17420
Destructing ref 0x0
MyObject[3]
Destructing ref 0x7ff7d9741800
Destructing ref 0x7ff7d9742150
Destructing ref 0x7ffdb2c17420
Destructing ref 0x7ffdb2c167a0
MyObject[2] destructor
Destructing ref 0x7ff7d9742750
Destructing ref 0x7ffdb2c16a50
MyObject[1] destructor
MyObject[4] constructor
Initialized ref from pointer 0x7ff7d9742750
Initialized ref from pointer 0x7ffdb2c16a50
MyObject[5] constructor
Initialized ref from pointer 0x7ff7d9742150
Initialized ref from pointer 0x7ff7d9742150
Destructing ref 0x7ff7d9742150
Initialized ref from pointer 0x7ffdb2c167a0
Initialized ref from pointer 0x7ffdb2c167a0
Destructing ref 0x7ffdb2c167a0
MyObject[6] constructor
Initialized ref from pointer 0x7ff7d9741cf0
Destructing ref 0x7ff7d9741800
Initialized ref from pointer 0x7ffdb2c161d0
Destructing ref 0x7ffdb2c17420
MyObject[3] destructor
<example.MyObject object at 0x10297a360>
<example.MyObject object at 0x10cf67600>
MyObject[4]
Created empty ref
Initialized ref from pointer 0x7ff7d9742750
Move-assigning ref 0x7ff7d9742750
Initialized ref from pointer 0x7ffdb2c16a50
Move-assigning ref 0x7ffdb2c16a50
Destructing ref 0x0
Initialized ref from ref 0x7ff7d9742750
Initialized ref from ref 0x7ffdb2c16a50
MyObject[4]
Destructing ref 0x7ff7d9742750
Destructing ref 0x7ff7d9742750
Destructing ref 0x7ffdb2c16a50
Destructing ref 0x7ffdb2c16a50
Created empty ref
Initialized ref from pointer 0x7ff7d9742750
Move-assigning ref 0x7ff7d9742750
Initialized ref from pointer 0x7ffdb2c16a50
Move-assigning ref 0x7ffdb2c16a50
Destructing ref 0x0
MyObject[4]
Destructing ref 0x7ff7d9742750
Destructing ref 0x7ffdb2c16a50
Created empty ref
Initialized ref from pointer 0x7ff7d9742750
Move-assigning ref 0x7ff7d9742750
Initialized ref from pointer 0x7ffdb2c16a50
Move-assigning ref 0x7ffdb2c16a50
Destructing ref 0x0
MyObject[4]
Destructing ref 0x7ff7d9742750
Destructing ref 0x7ffdb2c16a50
MyObject[4]
Created empty ref
Initialized ref from pointer 0x7ff7d9742750
Move-assigning ref 0x7ff7d9742750
Initialized ref from pointer 0x7ffdb2c16a50
Move-assigning ref 0x7ffdb2c16a50
Destructing ref 0x0
Initialized ref from ref 0x7ff7d9742750
Initialized ref from ref 0x7ffdb2c16a50
MyObject[4]
Destructing ref 0x7ff7d9742750
Destructing ref 0x7ff7d9742750
Destructing ref 0x7ffdb2c16a50
Destructing ref 0x7ffdb2c16a50
Created empty ref
Initialized ref from pointer 0x7ff7d9742750
Move-assigning ref 0x7ff7d9742750
Initialized ref from pointer 0x7ffdb2c16a50
Move-assigning ref 0x7ffdb2c16a50
Destructing ref 0x0
MyObject[4]
Destructing ref 0x7ff7d9742750
Destructing ref 0x7ffdb2c16a50
Created empty ref
Initialized ref from pointer 0x7ff7d9742750
Move-assigning ref 0x7ff7d9742750
Initialized ref from pointer 0x7ffdb2c16a50
Move-assigning ref 0x7ffdb2c16a50
Destructing ref 0x0
MyObject[4]
Destructing ref 0x7ff7d9742750
<example.MyObject object at 0x10297a870>
Destructing ref 0x7ffdb2c16a50
<example.MyObject object at 0x10cf7d210>
MyObject[5]
Created empty ref
Initialized ref from pointer 0x7ff7d9742150
Move-assigning ref 0x7ff7d9742150
Initialized ref from pointer 0x7ffdb2c167a0
Move-assigning ref 0x7ffdb2c167a0
Destructing ref 0x0
Initialized ref from ref 0x7ff7d9742150
Initialized ref from ref 0x7ffdb2c167a0
MyObject[5]
Destructing ref 0x7ff7d9742150
Destructing ref 0x7ff7d9742150
Destructing ref 0x7ffdb2c167a0
Destructing ref 0x7ffdb2c167a0
Created empty ref
Initialized ref from pointer 0x7ff7d9742150
Move-assigning ref 0x7ff7d9742150
Initialized ref from pointer 0x7ffdb2c167a0
Move-assigning ref 0x7ffdb2c167a0
Destructing ref 0x0
MyObject[5]
Destructing ref 0x7ff7d9742150
Destructing ref 0x7ffdb2c167a0
Created empty ref
Initialized ref from pointer 0x7ff7d9742150
Move-assigning ref 0x7ff7d9742150
Initialized ref from pointer 0x7ffdb2c167a0
Move-assigning ref 0x7ffdb2c167a0
Destructing ref 0x0
MyObject[5]
Destructing ref 0x7ff7d9742150
Destructing ref 0x7ffdb2c167a0
MyObject[5]
Created empty ref
Initialized ref from pointer 0x7ff7d9742150
Move-assigning ref 0x7ff7d9742150
Initialized ref from pointer 0x7ffdb2c167a0
Move-assigning ref 0x7ffdb2c167a0
Destructing ref 0x0
Initialized ref from ref 0x7ff7d9742150
Initialized ref from ref 0x7ffdb2c167a0
MyObject[5]
Destructing ref 0x7ff7d9742150
Destructing ref 0x7ff7d9742150
Destructing ref 0x7ffdb2c167a0
Destructing ref 0x7ffdb2c167a0
Created empty ref
Initialized ref from pointer 0x7ff7d9742150
Move-assigning ref 0x7ff7d9742150
Initialized ref from pointer 0x7ffdb2c167a0
Move-assigning ref 0x7ffdb2c167a0
Destructing ref 0x0
MyObject[5]
Destructing ref 0x7ff7d9742150
Destructing ref 0x7ffdb2c167a0
Created empty ref
Initialized ref from pointer 0x7ff7d9742150
Move-assigning ref 0x7ff7d9742150
Initialized ref from pointer 0x7ffdb2c167a0
Move-assigning ref 0x7ffdb2c167a0
Destructing ref 0x0
MyObject[5]
Destructing ref 0x7ff7d9742150
<example.MyObject object at 0x10297a8d0>
Destructing ref 0x7ffdb2c167a0
<example.MyObject object at 0x10cf7d270>
MyObject[6]
Created empty ref
Initialized ref from pointer 0x7ff7d9741cf0
Move-assigning ref 0x7ff7d9741cf0
Initialized ref from pointer 0x7ffdb2c161d0
Move-assigning ref 0x7ffdb2c161d0
Destructing ref 0x0
Initialized ref from ref 0x7ff7d9741cf0
Initialized ref from ref 0x7ffdb2c161d0
MyObject[6]
Destructing ref 0x7ff7d9741cf0
Destructing ref 0x7ff7d9741cf0
Destructing ref 0x7ffdb2c161d0
Destructing ref 0x7ffdb2c161d0
Created empty ref
Initialized ref from pointer 0x7ff7d9741cf0
Move-assigning ref 0x7ff7d9741cf0
Initialized ref from pointer 0x7ffdb2c161d0
Move-assigning ref 0x7ffdb2c161d0
Destructing ref 0x0
MyObject[6]
Destructing ref 0x7ff7d9741cf0
Destructing ref 0x7ffdb2c161d0
Created empty ref
Initialized ref from pointer 0x7ff7d9741cf0
Move-assigning ref 0x7ff7d9741cf0
Initialized ref from pointer 0x7ffdb2c161d0
Move-assigning ref 0x7ffdb2c161d0
Destructing ref 0x0
MyObject[6]
Destructing ref 0x7ff7d9741cf0
Destructing ref 0x7ffdb2c161d0
MyObject[6]
Created empty ref
Initialized ref from pointer 0x7ff7d9741cf0
Move-assigning ref 0x7ff7d9741cf0
Initialized ref from pointer 0x7ffdb2c161d0
Move-assigning ref 0x7ffdb2c161d0
Destructing ref 0x0
Initialized ref from ref 0x7ff7d9741cf0
Initialized ref from ref 0x7ffdb2c161d0
MyObject[6]
Destructing ref 0x7ff7d9741cf0
Destructing ref 0x7ff7d9741cf0
Destructing ref 0x7ffdb2c161d0
Destructing ref 0x7ffdb2c161d0
Created empty ref
Initialized ref from pointer 0x7ff7d9741cf0
Move-assigning ref 0x7ff7d9741cf0
Initialized ref from pointer 0x7ffdb2c161d0
Move-assigning ref 0x7ffdb2c161d0
Destructing ref 0x0
MyObject[6]
Destructing ref 0x7ff7d9741cf0
Destructing ref 0x7ffdb2c161d0
Created empty ref
Initialized ref from pointer 0x7ff7d9741cf0
Move-assigning ref 0x7ff7d9741cf0
Initialized ref from pointer 0x7ffdb2c161d0
Move-assigning ref 0x7ffdb2c161d0
Destructing ref 0x0
MyObject[6]
Destructing ref 0x7ff7d9741cf0
Destructing ref 0x7ffdb2c161d0
7
MyObject[7] constructor
Initialized ref from pointer 0x7ff7d9741800
Initialized ref from pointer 0x7ffdb2c17420
MyObject[7]
Destructing ref 0x7ff7d9741800
Destructing ref 0x7ffdb2c17420
MyObject[7] destructor
Created empty ref
MyObject[7] constructor
Initialized ref from pointer 0x7ff7d9741800
Initialized ref from pointer 0x7ff7d9741800
Move-assigning ref 0x7ff7d9741800
Initialized ref from pointer 0x7ffdb2c17420
Initialized ref from pointer 0x7ffdb2c17420
Move-assigning ref 0x7ffdb2c17420
Destructing ref 0x0
Initialized ref from ref 0x7ff7d9741800
Initialized ref from ref 0x7ffdb2c17420
MyObject[7]
Destructing ref 0x7ff7d9741800
Destructing ref 0x7ff7d9741800
Destructing ref 0x7ff7d9741800
Destructing ref 0x7ffdb2c17420
Destructing ref 0x7ffdb2c17420
Destructing ref 0x7ffdb2c17420
MyObject[7] destructor
Created empty ref
MyObject[7] constructor
Initialized ref from pointer 0x7ff7d9741800
Initialized ref from pointer 0x7ff7d9741800
Move-assigning ref 0x7ff7d9741800
Initialized ref from pointer 0x7ffdb2c17420
Initialized ref from pointer 0x7ffdb2c17420
Move-assigning ref 0x7ffdb2c17420
Destructing ref 0x0
MyObject[7]
Destructing ref 0x7ff7d9741800
Destructing ref 0x7ff7d9741800
Destructing ref 0x7ffdb2c17420
Destructing ref 0x7ffdb2c17420
MyObject[7] destructor
Created empty ref
MyObject[7] constructor
Initialized ref from pointer 0x7ff7d9741800
Initialized ref from pointer 0x7ff7d9741800
Move-assigning ref 0x7ff7d9741800
Initialized ref from pointer 0x7ffdb2c17420
Initialized ref from pointer 0x7ffdb2c17420
Move-assigning ref 0x7ffdb2c17420
Destructing ref 0x0
MyObject[7]
Destructing ref 0x7ff7d9741800
Destructing ref 0x7ff7d9741800
Destructing ref 0x7ffdb2c17420
Destructing ref 0x7ffdb2c17420
MyObject[7] destructor
Destructing ref 0x7ff7d9741cf0
Destructing ref 0x7ffdb2c161d0
MyObject[6] destructor
Destructing ref 0x7ff7d9742150
Destructing ref 0x7ffdb2c167a0
MyObject[5] destructor
Destructing ref 0x7ff7d9742750
Destructing ref 0x7ffdb2c16a50
MyObject[4] destructor
MyObject2[1] constructor
MyObject2[2] constructor
<example.MyObject2 object at 0x10cf5c378>
MyObject2[1]
MyObject2[1]
MyObject2[1]
MyObject2[1]
<example.MyObject2 object at 0x10cf5c340>
MyObject2[2]
MyObject2[2]
MyObject2[2]
MyObject2[2]
MyObject2[1] destructor
MyObject2[2] destructor

View File

@ -517,12 +517,25 @@ protected:
template <typename type, typename holder_type> class type_caster_holder : public type_caster<type> {
public:
typedef type_caster<type> parent;
template <typename T = holder_type,
typename std::enable_if<std::is_same<std::shared_ptr<type>, T>::value, int>::type = 0>
bool load(PyObject *src, bool convert) {
if (!parent::load(src, convert))
return false;
holder = holder_type(((type *) parent::value)->shared_from_this());
return true;
}
template <typename T = holder_type,
typename std::enable_if<!std::is_same<std::shared_ptr<type>, T>::value, int>::type = 0>
bool load(PyObject *src, bool convert) {
if (!parent::load(src, convert))
return false;
holder = holder_type((type *) parent::value);
return true;
}
explicit operator type*() { return this->value; }
explicit operator type&() { return *(this->value); }
explicit operator holder_type&() { return holder; }

View File

@ -798,11 +798,29 @@ public:
return *this;
}
private:
template <typename T = holder_type,
typename std::enable_if<!std::is_same<std::shared_ptr<type>, T>::value, int>::type = 0>
static void init_holder(PyObject *inst_) {
instance_type *inst = (instance_type *) inst_;
new (&inst->holder) holder_type(inst->value);
inst->constructed = true;
}
template <typename T = holder_type,
typename std::enable_if<std::is_same<std::shared_ptr<type>, T>::value, int>::type = 0>
static void init_holder(PyObject *inst_) {
instance_type *inst = (instance_type *) inst_;
try {
new (&inst->holder) holder_type(
inst->value->shared_from_this()
);
} catch (const std::bad_weak_ptr &) {
new (&inst->holder) holder_type(inst->value);
}
inst->constructed = true;
}
static void dealloc(PyObject *inst_) {
instance_type *inst = (instance_type *) inst_;
if (inst->owned) {