2
0
mirror of https://github.com/boostorg/python.git synced 2026-01-19 04:22:16 +00:00

Add work-around to crash in ~object_base().

For the free-threaded build (and possibly the debug build), it is not
safe to call Py_DECREF() if there is no valid Python thread-state.
This commit is contained in:
Neil Schemenauer
2025-10-30 14:23:37 -07:00
parent cabb466057
commit 6f5f3b6607

View File

@@ -419,6 +419,16 @@ inline api::object_base& api::object_base::operator=(api::object_base const& rhs
inline api::object_base::~object_base()
{
#ifdef Py_GIL_DISABLED
// This is a not very elegant fix for a problem that occurs with the
// free-threaded build of Python. If this is called when the interpreter
// has already been finalized, the thread-state can be null. Unlike the
// GIL-enabled build, Py_DECREF() requires a valid thread-state. This
// causes a memory leak, rather than crash, which seems preferable.
if (PyThreadState_GetUnchecked() == NULL) {
return;
}
#endif
assert( Py_REFCNT(m_ptr) > 0 );
Py_DECREF(m_ptr);
}