Skip to content

Commit 7915de6

Browse files
BetsyMcPhailEricCousineau-TRI
authored andcommitted
Remove objects from the inactive overload cache when they are deregistered
Add TODOs
1 parent 69a5d92 commit 7915de6

File tree

2 files changed

+21
-2
lines changed

2 files changed

+21
-2
lines changed

include/pybind11/detail/class.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,18 @@ inline bool deregister_instance_impl(void *ptr, instance *self) {
225225
for (auto it = range.first; it != range.second; ++it) {
226226
if (self == it->second && Py_TYPE(self) == Py_TYPE(it->second)) {
227227
registered_instances.erase(it);
228+
// TODO(eric.cousineau): This section can be removed if we can use
229+
// a different key, as mentioned in `get_type_overload`.
230+
PyObject *to_remove = (PyObject *)it->second;
231+
auto &cache = detail::get_internals().inactive_overload_cache;
232+
for (auto key = cache.begin(); key != cache.end();) {
233+
if (key->first == to_remove) {
234+
key = cache.erase(key);
235+
}
236+
else {
237+
++key;
238+
}
239+
}
228240
return true;
229241
}
230242
}

include/pybind11/pybind11.h

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2410,8 +2410,15 @@ inline function get_type_overload(const void *this_ptr, const detail::type_info
24102410
handle self = detail::get_object_handle(this_ptr, this_type);
24112411
if (!self)
24122412
return function();
2413-
handle type = self.get_type();
2414-
auto key = std::make_pair(type.ptr(), name);
2413+
// N.B. Rather than use `type.ptr()` (which upstream uses), use
2414+
// `self.ptr()` for better uniqueness to avoid drake#11424.
2415+
// TODO(eric.cousineau): Consider using something like the tuple
2416+
// (cls, f"{cls.__module__}.{cls.__qualname__}", name). However, since
2417+
// internals::inactive_overload_cache only supports
2418+
// `(PyObject*, const char*)` (and thus avoids memory management), it may
2419+
// require some hand-crafting and really bumping the internals version. For
2420+
// now, leave as-is.
2421+
auto key = std::make_pair(self.ptr(), name);
24152422

24162423
/* Cache functions that aren't overloaded in Python to avoid
24172424
many costly Python dictionary lookups below */

0 commit comments

Comments
 (0)