mirror of
https://github.com/pybind/pybind11.git
synced 2026-06-07 08:15:22 +00:00
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.
This commit is contained in:
committed by
Wenzel Jakob
parent
e916d846bf
commit
c07ec31edf
@@ -48,6 +48,24 @@ class Dupe2 {};
|
||||
class Dupe3 {};
|
||||
class DupeException : public std::runtime_error {};
|
||||
|
||||
// #478
|
||||
template <typename T> class custom_unique_ptr {
|
||||
public:
|
||||
custom_unique_ptr() { print_default_created(this); }
|
||||
custom_unique_ptr(T *ptr) : _ptr{ptr} { print_created(this, ptr); }
|
||||
custom_unique_ptr(custom_unique_ptr<T> &&move) : _ptr{move._ptr} { move._ptr = nullptr; print_move_created(this); }
|
||||
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; }
|
||||
custom_unique_ptr(const custom_unique_ptr<T> &) = delete;
|
||||
void operator=(const custom_unique_ptr<T> ©) = delete;
|
||||
~custom_unique_ptr() { print_destroyed(this); if (_ptr) destruct_ptr(); }
|
||||
private:
|
||||
T *_ptr = nullptr;
|
||||
void destruct_ptr() { delete _ptr; }
|
||||
};
|
||||
PYBIND11_DECLARE_HOLDER_TYPE(T, custom_unique_ptr<T>);
|
||||
|
||||
|
||||
|
||||
void init_issues(py::module &m) {
|
||||
py::module m2 = m.def_submodule("issues");
|
||||
|
||||
@@ -311,6 +329,25 @@ void init_issues(py::module &m) {
|
||||
py::class_<SharedParent, std::shared_ptr<SharedParent>>(m, "SharedParent")
|
||||
.def(py::init<>())
|
||||
.def("get_child", &SharedParent::get_child, py::return_value_policy::reference);
|
||||
|
||||
/// Issue/PR #478: unique ptrs constructed and freed without destruction
|
||||
class SpecialHolderObj {
|
||||
public:
|
||||
int val = 0;
|
||||
SpecialHolderObj *ch = nullptr;
|
||||
SpecialHolderObj(int v, bool make_child = true) : val{v}, ch{make_child ? new SpecialHolderObj(val+1, false) : nullptr}
|
||||
{ print_created(this, val); }
|
||||
~SpecialHolderObj() { delete ch; print_destroyed(this); }
|
||||
SpecialHolderObj *child() { return ch; }
|
||||
};
|
||||
|
||||
py::class_<SpecialHolderObj, custom_unique_ptr<SpecialHolderObj>>(m, "SpecialHolderObj")
|
||||
.def(py::init<int>())
|
||||
.def("child", &SpecialHolderObj::child, pybind11::return_value_policy::reference_internal)
|
||||
.def_readwrite("val", &SpecialHolderObj::val)
|
||||
.def_static("holder_cstats", &ConstructorStats::get<custom_unique_ptr<SpecialHolderObj>>,
|
||||
py::return_value_policy::reference)
|
||||
;
|
||||
};
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user