Bugfix/Check actual value when deregistering pybind11 instance (#2252)
* Add tests demonstrating the problem with deregistering pybind11 instances
* Fix deregistering of different pybind11 instance from internals
Co-authored-by: Yannick Jadoul <yannick.jadoul@belgacom.net>
Co-authored-by: Blistic <wots_wot@hotmail.com>
diff --git a/tests/test_class.py b/tests/test_class.py
index fb061d1..38eb55f 100644
--- a/tests/test_class.py
+++ b/tests/test_class.py
@@ -368,3 +368,18 @@
def test_exception_rvalue_abort():
with pytest.raises(RuntimeError):
m.PyPrintDestructor().throw_something()
+
+
+# https://github.com/pybind/pybind11/issues/1568
+def test_multiple_instances_with_same_pointer(capture):
+ n = 100
+ instances = [m.SamePointer() for _ in range(n)]
+ for i in range(n):
+ # We need to reuse the same allocated memory for with a different type,
+ # to ensure the bug in `deregister_instance_impl` is detected. Otherwise
+ # `Py_TYPE(self) == Py_TYPE(it->second)` will still succeed, even though
+ # the `instance` is already deleted.
+ instances[i] = m.Empty()
+ # No assert: if this does not trigger the error
+ # pybind11_fail("pybind11_object_dealloc(): Tried to deallocate unregistered instance!");
+ # and just completes without crashing, we're good.