Skip to content

Commit c07ec31

Browse files
jagermanwjakob
authored andcommitted
Don't construct unique_ptr around unowned pointers (#478)
If we need to initialize a holder around an unowned instance, and the holder type is non-copyable (i.e. a unique_ptr), we currently construct the holder type around the value pointer, but then never actually destruct the holder: the holder destructor is called only for the instance that actually has `inst->owned = true` set. This seems no pointer, however, in creating such a holder around an unowned instance: we never actually intend to use anything that the unique_ptr gives us: and, in fact, do not want the unique_ptr (because if it ever actually got destroyed, it would cause destruction of the wrapped pointer, despite the fact that that wrapped pointer isn't owned). This commit changes the logic to only create a unique_ptr holder if we actually own the instance, and to destruct via the constructed holder whenever we have a constructed holder--which will now only be the case for owned-unique-holder or shared-holder types. Other changes include: * Added test for non-movable holder constructor/destructor counts The three alive assertions now pass, before #478 they fail with counts of 2/2/1 respectively, because of the unique_ptr that we don't want and don't destroy (because we don't *want* its destructor to run). * Return cstats reference; fix ConstructStats doc Small cleanup to the #478 test code, and fix to the ConstructStats documentation (the static method definition should use `reference` not `reference_internal`). * Rename inst->constructed to inst->holder_constructed This makes it clearer exactly what it's referring to.
1 parent e916d84 commit c07ec31

File tree

5 files changed

+68
-13
lines changed

5 files changed

+68
-13
lines changed

include/pybind11/common.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -311,7 +311,7 @@ template <typename type> struct instance_essentials {
311311
type *value;
312312
PyObject *weakrefs;
313313
bool owned : 1;
314-
bool constructed : 1;
314+
bool holder_constructed : 1;
315315
};
316316

317317
/// PyObject wrapper around generic types, includes a special holder type that is responsible for lifetime management

include/pybind11/pybind11.h

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -821,7 +821,7 @@ class generic_type : public object {
821821
auto tinfo = detail::get_type_info(type);
822822
self->value = ::operator new(tinfo->type_size);
823823
self->owned = true;
824-
self->constructed = false;
824+
self->holder_constructed = false;
825825
detail::get_internals().registered_instances.emplace(self->value, (PyObject *) self);
826826
return (PyObject *) self;
827827
}
@@ -1134,7 +1134,7 @@ class class_ : public detail::generic_type {
11341134
} catch (const std::bad_weak_ptr &) {
11351135
new (&inst->holder) holder_type(inst->value);
11361136
}
1137-
inst->owned = true;
1137+
inst->holder_constructed = true;
11381138
}
11391139

11401140
/// Initialize holder object, variant 2: try to construct from existing holder object, if possible
@@ -1145,31 +1145,32 @@ class class_ : public detail::generic_type {
11451145
new (&inst->holder) holder_type(*holder_ptr);
11461146
else
11471147
new (&inst->holder) holder_type(inst->value);
1148-
inst->owned = true;
1148+
inst->holder_constructed = true;
11491149
}
11501150

11511151
/// Initialize holder object, variant 3: holder is not copy constructible (e.g. unique_ptr), always initialize from raw pointer
11521152
template <typename T = holder_type,
11531153
detail::enable_if_t<!std::is_copy_constructible<T>::value, int> = 0>
11541154
static void init_holder_helper(instance_type *inst, const holder_type * /* unused */, const void * /* dummy */) {
1155-
new (&inst->holder) holder_type(inst->value);
1155+
if (inst->owned) {
1156+
new (&inst->holder) holder_type(inst->value);
1157+
inst->holder_constructed = true;
1158+
}
11561159
}
11571160

11581161
/// Initialize holder object of an instance, possibly given a pointer to an existing holder
11591162
static void init_holder(PyObject *inst_, const void *holder_ptr) {
11601163
auto inst = (instance_type *) inst_;
11611164
init_holder_helper(inst, (const holder_type *) holder_ptr, inst->value);
1162-
inst->constructed = true;
11631165
}
11641166

11651167
static void dealloc(PyObject *inst_) {
11661168
instance_type *inst = (instance_type *) inst_;
1167-
if (inst->owned) {
1168-
if (inst->constructed)
1169-
inst->holder.~holder_type();
1170-
else
1171-
::operator delete(inst->value);
1172-
}
1169+
if (inst->holder_constructed)
1170+
inst->holder.~holder_type();
1171+
else if (inst->owned)
1172+
::operator delete(inst->value);
1173+
11731174
generic_type::dealloc((detail::instance<void> *) inst);
11741175
}
11751176

tests/constructor_stats.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ from the ConstructorStats instance `.values()` method.
5656
In some cases, when you need to track instances of a C++ class not registered with pybind11, you
5757
need to add a function returning the ConstructorStats for the C++ class; this can be done with:
5858
59-
m.def("get_special_cstats", &ConstructorStats::get<SpecialClass>, py::return_value_policy::reference_internal)
59+
m.def("get_special_cstats", &ConstructorStats::get<SpecialClass>, py::return_value_policy::reference)
6060
6161
Finally, you can suppress the output messages, but keep the constructor tracking (for
6262
inspection/testing in python) by using the functions with `print_` replaced with `track_` (e.g.

tests/test_issues.cpp

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,24 @@ class Dupe2 {};
4848
class Dupe3 {};
4949
class DupeException : public std::runtime_error {};
5050

51+
// #478
52+
template <typename T> class custom_unique_ptr {
53+
public:
54+
custom_unique_ptr() { print_default_created(this); }
55+
custom_unique_ptr(T *ptr) : _ptr{ptr} { print_created(this, ptr); }
56+
custom_unique_ptr(custom_unique_ptr<T> &&move) : _ptr{move._ptr} { move._ptr = nullptr; print_move_created(this); }
57+
custom_unique_ptr &operator=(custom_unique_ptr<T> &&move) { print_move_assigned(this); if (_ptr) destruct_ptr(); _ptr = move._ptr; move._ptr = nullptr; return *this; }
58+
custom_unique_ptr(const custom_unique_ptr<T> &) = delete;
59+
void operator=(const custom_unique_ptr<T> &copy) = delete;
60+
~custom_unique_ptr() { print_destroyed(this); if (_ptr) destruct_ptr(); }
61+
private:
62+
T *_ptr = nullptr;
63+
void destruct_ptr() { delete _ptr; }
64+
};
65+
PYBIND11_DECLARE_HOLDER_TYPE(T, custom_unique_ptr<T>);
66+
67+
68+
5169
void init_issues(py::module &m) {
5270
py::module m2 = m.def_submodule("issues");
5371

@@ -311,6 +329,25 @@ void init_issues(py::module &m) {
311329
py::class_<SharedParent, std::shared_ptr<SharedParent>>(m, "SharedParent")
312330
.def(py::init<>())
313331
.def("get_child", &SharedParent::get_child, py::return_value_policy::reference);
332+
333+
/// Issue/PR #478: unique ptrs constructed and freed without destruction
334+
class SpecialHolderObj {
335+
public:
336+
int val = 0;
337+
SpecialHolderObj *ch = nullptr;
338+
SpecialHolderObj(int v, bool make_child = true) : val{v}, ch{make_child ? new SpecialHolderObj(val+1, false) : nullptr}
339+
{ print_created(this, val); }
340+
~SpecialHolderObj() { delete ch; print_destroyed(this); }
341+
SpecialHolderObj *child() { return ch; }
342+
};
343+
344+
py::class_<SpecialHolderObj, custom_unique_ptr<SpecialHolderObj>>(m, "SpecialHolderObj")
345+
.def(py::init<int>())
346+
.def("child", &SpecialHolderObj::child, pybind11::return_value_policy::reference_internal)
347+
.def_readwrite("val", &SpecialHolderObj::val)
348+
.def_static("holder_cstats", &ConstructorStats::get<custom_unique_ptr<SpecialHolderObj>>,
349+
py::return_value_policy::reference)
350+
;
314351
};
315352

316353

tests/test_issues.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,3 +201,20 @@ def test_enable_shared_from_this_with_reference_rvp():
201201
assert cstats.alive() == 1
202202
del child, parent
203203
assert cstats.alive() == 0
204+
205+
def test_non_destructed_holders():
206+
""" Issue #478: unique ptrs constructed and freed without destruction """
207+
from pybind11_tests import SpecialHolderObj
208+
209+
a = SpecialHolderObj(123)
210+
b = a.child()
211+
212+
assert a.val == 123
213+
assert b.val == 124
214+
215+
cstats = SpecialHolderObj.holder_cstats()
216+
assert cstats.alive() == 1
217+
del b
218+
assert cstats.alive() == 1
219+
del a
220+
assert cstats.alive() == 0

0 commit comments

Comments
 (0)