Skip to content

Commit 6f5f3b6

Browse files
committed
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.
1 parent cabb466 commit 6f5f3b6

File tree

1 file changed

+10
-0
lines changed

1 file changed

+10
-0
lines changed

include/boost/python/object_core.hpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -419,6 +419,16 @@ inline api::object_base& api::object_base::operator=(api::object_base const& rhs
419419

420420
inline api::object_base::~object_base()
421421
{
422+
#ifdef Py_GIL_DISABLED
423+
// This is a not very elegant fix for a problem that occurs with the
424+
// free-threaded build of Python. If this is called when the interpreter
425+
// has already been finalized, the thread-state can be null. Unlike the
426+
// GIL-enabled build, Py_DECREF() requires a valid thread-state. This
427+
// causes a memory leak, rather than crash, which seems preferable.
428+
if (PyThreadState_GetUnchecked() == NULL) {
429+
return;
430+
}
431+
#endif
422432
assert( Py_REFCNT(m_ptr) > 0 );
423433
Py_DECREF(m_ptr);
424434
}

0 commit comments

Comments
 (0)