Skip to content
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

Make sure to properly untrack gc objects before freeing them #4461

Merged
merged 4 commits into from
Jan 18, 2023
Merged
Changes from 3 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
8 changes: 7 additions & 1 deletion include/pybind11/detail/class.h
Original file line number Diff line number Diff line change
Expand Up @@ -445,9 +445,15 @@ inline void clear_instance(PyObject *self) {
/// Instance destructor function for all pybind11 types. It calls `type_info.dealloc`
/// to destroy the C++ object itself, while the rest is Python bookkeeping.
extern "C" inline void pybind11_object_dealloc(PyObject *self) {
auto *type = Py_TYPE(self);
albanD marked this conversation as resolved.
Show resolved Hide resolved

// If this is a GC tracked object, untrack it first
if (PyType_HasFeature(type, Py_TPFLAGS_HAVE_GC) != 0) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you please add your sentence from the PR description as a comment here?

// Note that the track call is implicitly done by the default tp_alloc, which we never override.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be good now!

PyObject_GC_UnTrack(self);
}

clear_instance(self);

auto *type = Py_TYPE(self);
type->tp_free(self);

#if PY_VERSION_HEX < 0x03080000
Expand Down