Closed
Description
The following code segfaults on Python 3.9 and macOS:
from pybind11_tests import gil_scoped as m
def _python_to_cpp_to_python():
"""Calls different C++ functions that come back to Python."""
class ExtendedVirtClass(m.VirtClass):
def virtual_func(self):
pass
def pure_virtual_func(self):
pass
extended = ExtendedVirtClass()
m.test_callback_py_obj(lambda: None)
m.test_callback_std_func(lambda: None)
m.test_callback_virtual_func(extended)
m.test_callback_pure_virtual_func(extended)
for x in range(1000):
_python_to_cpp_to_python()
Debug output:
* thread #2, queue = 'com.apple.main-thread', stop reason = EXC_BAD_ACCESS (code=1, address=0x74736e6f43)
* frame #0: 0x000000010017434a Python`meth_dealloc + 120
frame #1: 0x000000010014645a Python`instancemethod_dealloc + 65
frame #2: 0x0000000100168feb Python`PyDict_Clear + 409
frame #3: 0x00000001001866ec Python`type_clear + 59
frame #4: 0x0000000100233f00 Python`collect + 2558
frame #5: 0x00000001002334f1 Python`_PyGC_CollectNoFail + 67
frame #6: 0x000000010020619c Python`_PyImport_Cleanup + 1545
frame #7: 0x0000000100216680 Python`Py_FinalizeEx + 160
frame #8: 0x000000010023279e Python`Py_RunMain + 1408
frame #9: 0x0000000100232c86 Python`pymain_main + 306
frame #10: 0x0000000100232cd4 Python`Py_BytesMain + 42
frame #11: 0x00007fff6910dcc9 libdyld.dylib`start + 1
Originally posted by @henryiii in #2391 (comment)