diff --git a/Modules/gcmodule.c b/Modules/gcmodule.c --- a/Modules/gcmodule.c +++ b/Modules/gcmodule.c @@ -780,24 +780,24 @@ finalize_garbage(PyGC_Head *collectable, PyGC_Head *old) { destructor finalize; - PyGC_Head *gc = collectable->gc.gc_next; + PyGC_Head buffer; - for (; gc != collectable; gc = gc->gc.gc_next) { + gc_list_init(&buffer); + + while (collectable->gc.gc_next != collectable) { + PyGC_Head *gc = collectable->gc.gc_next; PyObject *op = FROM_GC(gc); - + gc_list_move(gc, &buffer); if (!_PyGCHead_FINALIZED(gc) && PyType_HasFeature(Py_TYPE(op), Py_TPFLAGS_HAVE_FINALIZE) && (finalize = Py_TYPE(op)->tp_finalize) != NULL) { _PyGCHead_SET_FINALIZED(gc, 1); Py_INCREF(op); finalize(op); - if (Py_REFCNT(op) == 1) { - /* op will be destroyed */ - gc = gc->gc.gc_prev; - } Py_DECREF(op); } } + gc_list_merge(&buffer, collectable); } /* Walk the collectable list and check that they are really unreachable