Battle of the garbage collectors, or ARGGHHHHHH!!!!

CFK cfkaran2 at gmail.com
Mon Apr 24 18:24:17 EDT 2017


TLDR version: the bdwgc garbage collector (http://www.hboehm.info/gc/) and
python's collector are not playing nice with one another, and I need to
make them work with each other.

Long version: I'm trying to write bindings for python via ctypes to control
a library written in C that uses the bdwgc garbage collector (
http://www.hboehm.info/gc/).  The bindings mostly work, except for when
either bdwgc or python's garbage collector decide to get into an argument
over what is garbage and what isn't, in which case I get a segfault because
one or the other collector has already reaped the memory.  I need the two
sides to play nice with one another.  I can think of two solutions:

First, I can replace Python's garbage collector via the functions described
at https://docs.python.org/3/c-api/memory.html#customize-memory-allocators
so that they use the bdwgc functions instead.  However, this leads me to a
whole series of questions:

   1. Has anyone done anything like this before?  Is there any reason to
   believe it won't work?
   2. Since I'm going through ctypes, the python interpreter will be up and
   running before my library's code will be called.  I'm guessing that this
   will lead to horribleness, but I'm hoping that python is able to do better
   than that somehow.

Second, is to hope that there is some way of getting memory from python,
use it in C, and let the python garbage collector deal with it (essentially
replacing bdwgc in the C code with python's garbage collector).

I don't have a great deal of hope for either method working, but I'm hoping
I'm wrong, and that someone can save me from the headaches I'm having.  Is
there hope, or am I stuck?


Thanks,

Cem Karan


More information about the Python-list mailing list