Skip to content

Instantly share code, notes, and snippets.

@colesbury
Created June 24, 2020 22:32
Show Gist options
  • Select an option

  • Save colesbury/76ad12a895449883b97a730deefb1e6c to your computer and use it in GitHub Desktop.

Select an option

Save colesbury/76ad12a895449883b97a730deefb1e6c to your computer and use it in GitHub Desktop.
diff --git a/include/pybind11/detail/class.h b/include/pybind11/detail/class.h
index 45697f2..027a717 100644
--- a/include/pybind11/detail/class.h
+++ b/include/pybind11/detail/class.h
@@ -216,19 +216,24 @@ inline void traverse_offset_bases(void *valueptr, const detail::type_info *tinfo
}
inline bool register_instance_impl(void *ptr, instance *self) {
- get_internals().registered_instances.emplace(ptr, self);
+ with_internals([&](internals &internals) {
+ internals.registered_instances.emplace(ptr, self);
+ });
return true; // unused, but gives the same signature as the deregister func
}
inline bool deregister_instance_impl(void *ptr, instance *self) {
- auto &registered_instances = get_internals().registered_instances;
- auto range = registered_instances.equal_range(ptr);
- for (auto it = range.first; it != range.second; ++it) {
- if (Py_TYPE(self) == Py_TYPE(it->second)) {
- registered_instances.erase(it);
- return true;
+ bool ret = false;
+ with_internals([&](internals &internals) {
+ auto &registered_instances = internals.registered_instances;
+ auto range = registered_instances.equal_range(ptr);
+ for (auto it = range.first; it != range.second; ++it) {
+ if (Py_TYPE(self) == Py_TYPE(it->second)) {
+ registered_instances.erase(it);
+ ret = true;
+ }
}
- }
- return false;
+ });
+ return ret;
}
inline void register_instance(instance *self, void *valptr, const type_info *tinfo) {
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment