Skip to content

Commit 4f4a3e6

Browse files
RobinD42jpe
authored andcommitted
Fix possible conflict with garbage collector.
Ensure that the GC is no longer tracking object an object before starting to delete it. Since Shiboken's object deletion goes through several steps we need to ensure that the GC will not try to delete the same object. Change-Id: Ia3337c72204b0ebf524959e1c99fbef7c1a02249 Reviewed-by: John Ehresman <jpe@wingware.com>
1 parent e2288e2 commit 4f4a3e6

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

libshiboken/basewrapper.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,14 @@ void SbkDeallocWrapper(PyObject* pyObj)
207207
{
208208
SbkObject* sbkObj = reinterpret_cast<SbkObject*>(pyObj);
209209

210+
// Ensure that the GC is no longer tracking this object to avoid a
211+
// possible reentrancy problem. Since there are multiple steps involved
212+
// in deallocating a SbkObject it is possible for the garbage collector to
213+
// be invoked and it trying to delete this object while it is still in
214+
// progress from the first time around, resulting in a double delete and a
215+
// crash.
216+
PyObject_GC_UnTrack(pyObj);
217+
210218
// Check that Python is still initialized as sometimes this is called by a static destructor
211219
// after Python interpeter is shutdown.
212220
if (sbkObj->weakreflist && Py_IsInitialized())
@@ -235,6 +243,15 @@ void SbkDeallocWrapper(PyObject* pyObj)
235243
void SbkDeallocWrapperWithPrivateDtor(PyObject* self)
236244
{
237245
SbkObject* sbkObj = reinterpret_cast<SbkObject*>(self);
246+
247+
// Ensure that the GC is no longer tracking this object to avoid a
248+
// possible reentrancy problem. Since there are multiple steps involved
249+
// in deallocating a SbkObject it is possible for the garbage collector to
250+
// be invoked and it trying to delete this object while it is still in
251+
// progress from the first time around, resulting in a double delete and a
252+
// crash.
253+
PyObject_GC_UnTrack(self);
254+
238255
// Check that Python is still initialized as sometimes this is called by a static destructor
239256
// after Python interpeter is shutdown.
240257
if (sbkObj->weakreflist && Py_IsInitialized())

0 commit comments

Comments
 (0)