_PyMem_DebugRawFree:错误 ID:使用 API '' 分配,使用 API 'o' 验证

_PyMem_DebugRawFree: bad ID: Allocated using API '', verified using API 'o'

我的程序因 CPython 的以下消息而崩溃

Debug memory block at address p=0x8a6e80: API ''
    0 bytes originally requested
    The 7 pad bytes at p-7 are not all FORBIDDENBYTE (0xfd):
        at p-7: 0x00 *** OUCH
        at p-6: 0x00 *** OUCH
        at p-5: 0x00 *** OUCH
        at p-4: 0x00 *** OUCH
        at p-3: 0x00 *** OUCH
        at p-2: 0x00 *** OUCH
        at p-1: 0x00 *** OUCH
    Because memory is corrupted at the start, the count of bytes requested
       may be bogus, and checking the trailing pad bytes may segfault.
    The 8 pad bytes at tail=0x8a6e80 are not all FORBIDDENBYTE (0xfd):
        at tail+0: 0x00 *** OUCH
        at tail+1: 0x00 *** OUCH
        at tail+2: 0x00 *** OUCH
        at tail+3: 0x00 *** OUCH
        at tail+4: 0x00 *** OUCH
        at tail+5: 0x00 *** OUCH
        at tail+6: 0x00 *** OUCH
        at tail+7: 0x00 *** OUCH

Enable tracemalloc to get the memory block allocation traceback

Fatal Python error: _PyMem_DebugRawFree: bad ID: Allocated using API '', verified using API 'o'
Python runtime state: finalizing (tstate=0x0000000001f43c50)

Current thread 0x00007f3f562fa740 (most recent call first):
  Garbage-collecting
<no Python frame>

程序的堆栈跟踪是

(gdb) bt
#0  0x00007f8575deeb43 in _PyObject_DebugDumpAddress (p=0x503520 <_Py_TrueStruct>) at /usr/src/debug/python3.10-3.10.2-1.fc35.x86_64/Objects/obmalloc.c:2773
#1  0x00007f8575dee7c8 in _PyMem_DebugCheckAddress (func=0x7f8576056e70 <__func__.8.lto_priv.4> "_PyMem_DebugRawFree", api=111 'o', p=0x503520 <_Py_TrueStruct>) at /usr/src/debug/python3.10-3.10.2-1.fc35.x86_64/Objects/obmalloc.c:2691
#2  0x00007f8575dee1e3 in _PyMem_DebugRawFree (ctx=0x7f857616e5e0 <_PyMem_Debug+96>, p=0x503520 <_Py_TrueStruct>) at /usr/src/debug/python3.10-3.10.2-1.fc35.x86_64/Objects/obmalloc.c:2524
#3  0x00007f8575dee710 in _PyMem_DebugFree (ctx=0x7f857616e5e0 <_PyMem_Debug+96>, ptr=0x503520 <_Py_TrueStruct>) at /usr/src/debug/python3.10-3.10.2-1.fc35.x86_64/Objects/obmalloc.c:2661
#4  0x00007f8575debe51 in PyObject_Free (ptr=0x503520 <_Py_TrueStruct>) at /usr/src/debug/python3.10-3.10.2-1.fc35.x86_64/Objects/obmalloc.c:709
#5  0x00007f8575e0d687 in object_dealloc (self=0x503520 <_Py_TrueStruct>) at /usr/src/debug/python3.10-3.10.2-1.fc35.x86_64/Objects/typeobject.c:4510
#6  0x00007f8575deaa0f in _Py_Dealloc (op=0x503520 <_Py_TrueStruct>) at /usr/src/debug/python3.10-3.10.2-1.fc35.x86_64/Objects/object.c:2288
#7  0x00007f8575dfe879 in _Py_DECREF (filename=0x7f85760c9d30 "/builddir/build/BUILD/Python-3.10.2/Include/object.h", lineno=567, op=0x503520 <_Py_TrueStruct>) at /usr/src/debug/python3.10-3.10.2-1.fc35.x86_64/Include/object.h:500
#8  0x00007f8575dfe8aa in _Py_XDECREF (op=0x503520 <_Py_TrueStruct>) at /usr/src/debug/python3.10-3.10.2-1.fc35.x86_64/Include/object.h:567
#9  0x00007f8575dff99b in tupledealloc (op=0x7f85679e0f80) at /usr/src/debug/python3.10-3.10.2-1.fc35.x86_64/Objects/tupleobject.c:276
#10 0x00007f8575deaa0f in _Py_Dealloc (op=0x7f85679e0f80) at /usr/src/debug/python3.10-3.10.2-1.fc35.x86_64/Objects/object.c:2288
#11 0x00007f8575d76263 in _Py_DECREF (filename=0x7f85760c9d30 "/builddir/build/BUILD/Python-3.10.2/Include/object.h", lineno=567, op=0x7f85679e0f80) at /usr/src/debug/python3.10-3.10.2-1.fc35.x86_64/Include/object.h:500
#12 0x00007f8575d76294 in _Py_XDECREF (op=0x7f85679e0f80) at /usr/src/debug/python3.10-3.10.2-1.fc35.x86_64/Include/object.h:567
#13 0x00007f8575d79eb5 in code_dealloc (co=0x7f85679f9080) at /usr/src/debug/python3.10-3.10.2-1.fc35.x86_64/Objects/codeobject.c:658
#14 0x00007f8575deaa0f in _Py_Dealloc (op=0x7f85679f9080) at /usr/src/debug/python3.10-3.10.2-1.fc35.x86_64/Objects/object.c:2288
#15 0x00007f8575da3190 in _Py_DECREF (filename=0x7f85760dcf08 "/builddir/build/BUILD/Python-3.10.2/Objects/funcobject.c", lineno=623, op=0x7f85679f9080) at /usr/src/debug/python3.10-3.10.2-1.fc35.x86_64/Include/object.h:500
#16 0x00007f8575da5239 in func_clear (op=0x7f8567a1dff0) at /usr/src/debug/python3.10-3.10.2-1.fc35.x86_64/Objects/funcobject.c:623
#17 0x00007f8575da5510 in func_dealloc (op=0x7f8567a1dff0) at /usr/src/debug/python3.10-3.10.2-1.fc35.x86_64/Objects/funcobject.c:645
#18 0x00007f8575deaa0f in _Py_Dealloc (op=0x7f8567a1dff0) at /usr/src/debug/python3.10-3.10.2-1.fc35.x86_64/Objects/object.c:2288
#19 0x00007f8575dc36cf in _Py_DECREF (filename=0x7f85760c9d30 "/builddir/build/BUILD/Python-3.10.2/Include/object.h", lineno=567, op=0x7f8567a1dff0) at /usr/src/debug/python3.10-3.10.2-1.fc35.x86_64/Include/object.h:500
#20 0x00007f8575dc3722 in _Py_XDECREF (op=0x7f8567a1dff0) at /usr/src/debug/python3.10-3.10.2-1.fc35.x86_64/Include/object.h:567
#21 0x00007f8575dc57d8 in free_keys_object (keys=0x1c84a50) at /usr/src/debug/python3.10-3.10.2-1.fc35.x86_64/Objects/dictobject.c:628
#22 0x00007f8575dc4b85 in dictkeys_decref (dk=0x1c84a50) at /usr/src/debug/python3.10-3.10.2-1.fc35.x86_64/Objects/dictobject.c:337
#23 0x00007f8575dc943d in PyDict_Clear (op=0x7f8567a17bf0) at /usr/src/debug/python3.10-3.10.2-1.fc35.x86_64/Objects/dictobject.c:1807
#24 0x00007f8575e0d17e in type_clear (type=0x1c7c340) at /usr/src/debug/python3.10-3.10.2-1.fc35.x86_64/Objects/typeobject.c:4305
#25 0x00007f8575f951ed in delete_garbage (tstate=0x1b29f80, gcstate=0x1b0e158, collectable=0x7fff4fcf7140, old=0x1b0e1a0) at /usr/src/debug/python3.10-3.10.2-1.fc35.x86_64/Modules/gcmodule.c:1017
#26 0x00007f8575f95942 in gc_collect_main (tstate=0x1b29f80, generation=2, n_collected=0x0, n_uncollectable=0x0, nofail=1) at /usr/src/debug/python3.10-3.10.2-1.fc35.x86_64/Modules/gcmodule.c:1300
#27 0x00007f8575f9756d in _PyGC_CollectNoFail (tstate=0x1b29f80) at /usr/src/debug/python3.10-3.10.2-1.fc35.x86_64/Modules/gcmodule.c:2123
#28 0x00007f8575f5b51e in interpreter_clear (interp=0x1b0def0, tstate=0x1b29f80) at /usr/src/debug/python3.10-3.10.2-1.fc35.x86_64/Python/pystate.c:329
#29 0x00007f8575f5b63e in _PyInterpreterState_Clear (tstate=0x1b29f80) at /usr/src/debug/python3.10-3.10.2-1.fc35.x86_64/Python/pystate.c:361
#30 0x00007f8575f57ca4 in finalize_interp_clear (tstate=0x1b29f80) at /usr/src/debug/python3.10-3.10.2-1.fc35.x86_64/Python/pylifecycle.c:1665
#31 0x00007f8575f57e46 in Py_FinalizeEx () at /usr/src/debug/python3.10-3.10.2-1.fc35.x86_64/Python/pylifecycle.c:1843
#32 0x00007f8575f57e91 in Py_Finalize () at /usr/src/debug/python3.10-3.10.2-1.fc35.x86_64/Python/pylifecycle.c:1870
#33 0x000000000046b08c in qd_python_finalize () at /home/jdanek/repos/qpid/qpid-dispatch/src/python_embedded.c:83
#34 0x000000000044ff4f in qd_dispatch_free (qd=0x1b09690) at /home/jdanek/repos/qpid/qpid-dispatch/src/dispatch.c:380
#35 0x00000000004bea88 in main_process (config_path=0x7fff4fcf8a4e "conn-limit-router.conf", python_pkgdir=0x7fff4fcf8a68 "/home/jdanek/repos/qpid/qpid-dispatch/python", test_hooks=false, fd=2)
    at /home/jdanek/repos/qpid/qpid-dispatch/router/src/main.c:119
#36 0x00000000004bf531 in main (argc=5, argv=0x7fff4fcf74e8) at /home/jdanek/repos/qpid/qpid-dispatch/router/src/main.c:369

是什么导致了崩溃?我如何详细调查它?我该如何解决?

根据我的经验,Allocated using API '', verified using API 'o' 错误表明您的程序中某处存在引用计数错误。

It's easy to add or forget a Py_INCREF or Py_DECREF. Adding Py_DECREF(Py_True) or Py_DECREF(Py_False) by mistake

因此,首先回顾一下您对 Py_TruePy_False 以及 Py_None 的所有用法。

Python 当您使用调试版本的 libpython 时,3.11 将针对此类问题提供更具指示性的消息。

来源:https://bugs.python.org/issue45061