Skip to content

Commit ab6545a

Browse files
committed
Added check on iterator end position
1 parent aa98d95 commit ab6545a

File tree

1 file changed

+13
-9
lines changed

1 file changed

+13
-9
lines changed

include/pybind11/detail/class.h

+13-9
Original file line numberDiff line numberDiff line change
@@ -388,15 +388,19 @@ inline void clear_patients(PyObject *self) {
388388
auto *instance = reinterpret_cast<detail::instance *>(self);
389389
auto &internals = get_internals();
390390
auto pos = internals.patients.find(self);
391-
assert(pos != internals.patients.end());
392-
// Clearing the patients can cause more Python code to run, which
393-
// can invalidate the iterator. Extract the vector of patients
394-
// from the unordered_map first.
395-
auto patients = std::move(pos->second);
396-
internals.patients.erase(pos);
397-
instance->has_patients = false;
398-
for (PyObject *&patient : patients) {
399-
Py_CLEAR(patient);
391+
392+
if (pos != internals.patients.end()) {
393+
// Clearing the patients can cause more Python code to run, which
394+
// can invalidate the iterator. Extract the vector of patients
395+
// from the unordered_map first.
396+
auto patients = std::move(pos->second);
397+
internals.patients.erase(pos);
398+
instance->has_patients = false;
399+
for (PyObject *&patient : patients) {
400+
Py_CLEAR(patient);
401+
}
402+
} else {
403+
assert(pos != internals.patients.end());
400404
}
401405
}
402406

0 commit comments

Comments
 (0)