Skip to content

Inherit weakref from handle #4925

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/advanced/misc.rst
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@ avoids this issue involves weak reference with a cleanup callback:
);

// Create a weak reference with a cleanup callback and initially leak it
(void) py::weakref(m.attr("BaseClass"), cleanup_callback).release();
(void) py::weakref(m.attr("BaseClass"), cleanup_callback);

.. note::

Expand Down
7 changes: 2 additions & 5 deletions include/pybind11/pybind11.h
Original file line number Diff line number Diff line change
Expand Up @@ -1694,8 +1694,7 @@ class class_ : public detail::generic_type {
weakref(m_ptr, cpp_function([ptr](handle wr) {
delete ptr;
wr.dec_ref();
}))
.release();
}));
return *this;
}

Expand Down Expand Up @@ -2290,7 +2289,6 @@ PYBIND11_NOINLINE void keep_alive_impl(handle nurse, handle patient) {
weakref wr(nurse, disable_lifesupport);

patient.inc_ref(); /* reference patient and leak the weak reference */
(void) wr.release();
}
}

Expand Down Expand Up @@ -2338,8 +2336,7 @@ all_type_info_get_cache(PyTypeObject *type) {
}

wr.dec_ref();
}))
.release();
}));
}

return res;
Expand Down
1 change: 1 addition & 0 deletions include/pybind11/pytypes.h
Original file line number Diff line number Diff line change
Expand Up @@ -1875,6 +1875,7 @@ class weakref : public object {
pybind11_fail("Could not allocate weak reference!");
}
}
~weakref() { release(); }

private:
static PyObject *raw_weakref(PyObject *o) { return PyWeakref_NewRef(o, nullptr); }
Expand Down