The following code works fine and prints out an empty list (on python3.4
):
import gc
# code interfacing with C and cython
print(gc.garbage, flush=True)
Appending this line to the very end makes it segfault:
gc.collect()
It seems like it is pure luck that the automatic collection does not happen on its own (it took me some time to get reproducible error). The collection is not ran at "end of program" either because I am running this interactively.
Some googling led me to believe that some of the C/Cython code is creating/destroying objects without telling python that those objects are already removed from memory. Then python tries to remove them and crashes.
How can I find what those objects are? I thought that they would be in gc.garbage
before the crash inducing gc.collect()
?
Or are my assumptions completely incorrect?
Prepending an gc.set_debug(gc.DEBUG_STATS | gc.DEBUG_LEAK)
to this code leads to a python exception instead of a segfault.
---> print(gc.garbage, flush=True)
gc.collect()
...
ReferenceError: weakly-referenced object no longer exists
Edit: changed/simplified after comments from one of the answers