Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit c6a3ff6

Browse files
committed
SF bug 578752: COUNT_ALLOCS vs heap types
Repair segfaults and infinite loops in COUNT_ALLOCS builds in the presence of new-style (heap-allocated) classes/types. Bugfix candidate. I'll backport this to 2.2. It's irrelevant in 2.1.
1 parent d1b2045 commit c6a3ff6

2 files changed

Lines changed: 17 additions & 0 deletions

File tree

Misc/NEWS

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,14 @@ Tools/Demos
294294

295295
Build
296296

297+
- A bug was fixed that could cause COUNT_ALLOCS builds to segfault, or
298+
get into infinite loops, when a new-style class got garbage-collected.
299+
Unfortunately, to avoid this, the way COUNT_ALLOCS works requires
300+
that new-style classes be immortal in COUNT_ALLOCS builds. Note that
301+
COUNT_ALLOCS is not enabled by default, in either release or debug
302+
builds, and that new-style classes are immortal only in COUNT_ALLOCS
303+
builds.
304+
297305
- Compiling out the cyclic garbage collector is no longer an option.
298306
The old symbol WITH_CYCLE_GC is now ignored, and Python.h arranges
299307
that it's always defined (for the benefit of any extension modules

Objects/object.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,15 @@ inc_count(PyTypeObject *tp)
7474
if (tp->tp_next != NULL) /* sanity check */
7575
Py_FatalError("XXX inc_count sanity check");
7676
tp->tp_next = type_list;
77+
/* Note that as of Python 2.2, heap-allocated type objects
78+
* can go away, but this code requires that they stay alive
79+
* until program exit. That's why we're careful with
80+
* refcounts here. type_list gets a new reference to tp,
81+
* while ownership of the reference type_list used to hold
82+
* (if any) was transferred to tp->tp_next in the line above.
83+
* tp is thus effectively immortal after this.
84+
*/
85+
Py_INCREF(tp);
7786
type_list = tp;
7887
}
7988
tp->tp_allocs++;

0 commit comments

Comments
 (0)