I'm using a C library that has a few memory issues. I've identified that sometimes it decreases the reference count too early, leading to segfaults, and managed to work around that by keeping a list of objects to inflate the reference count. On the other hand, I've also identified specific cases where the reference count is too high. Is there any way to work around these cases, from Python code, by purposefully decreasing the reference count?
I understand this is hackish and in no way clean code or anything, but I've got to deal with the buggy C library somehow, and it seems this might be easier than figuring out where the issue in the C code is (luckily it's open-source), fixing it, then recompiling it on all the platforms I have to support.
weakref(docs.python.org/2/library/weakref.html) moduleweakrefwould not help in this case. The C code itself is failing to do aPy_DECREF- a weakref would only allow me not to increase the reference count any more than it is. Although, I suppose I could go looking for a reference to the object I already own and turn it into a weakref... hmm...Py_DecRefvia ctypes. See github.com/matplotlib/matplotlib/commit/… for an example that works around a memory leak in PySide.