Skip to content

Crash in debug builds of Python #50

@colesbury

Description

@colesbury

The following crashes in a debug build of Python:

import gc
import pycld2
gc.collect()
Modules/gcmodule.c:114: gc_decref: Assertion "gc_get_refs(g) > 0" failed: refcount is too small
Enable tracemalloc to get the memory block allocation traceback

object address  : 0x555555a2bf70
object refcount : 4
object type     : 0x555555966400
object type name: type
object repr     : <class 'pycld2.error'>

Fatal Python error: _PyObject_AssertFailed: _PyObject_AssertFailed
Python runtime state: initialized

Current thread 0x00007ffff7c33280 (most recent call first):
  File "<string>", line 1 in <module>
Aborted (core dumped)

The problem is that the module initialization creates two references tracked by the garbage collector, but one of them is missing a Py_INCREF. (The garbage collector works by subtracting incoming references from the reference count. In this case, the reference count is too small.)

st->error = PyErr_NewException("pycld2.error", NULL, NULL);

// Steals ref:
PyModule_AddObject(m, "error", st->error);

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions