Resources release issues
Created originally on Bitbucket by Gerard Marull-Paretas (Gerard Marull Paretas)
Hi,
I am facing some weird problems when releasing resources (cffi 1.11.0). My C library has pair functions to allocate/free multiple types of objects. In some case, Object B uses A internally, so what I do in Python is to keep a reference of A in B so that B is GC first (in C no refcount is implemented). I have tried these two things to release the resources:
- Use ffi.gc:
a = lib.create_a()
assert(a != ffi.NULL)
self._a = ffi.gc(a, lib.destroy_a)
- Use del
def __init__(self):
self._a = ffi.NULL
self._a = lib.create_a()
assert(self._a != ffi.NULL)
def __del__(self):
if self._a != ffi.NULL:
lib.destroy_a(self._a)
However, in some cases I get errors like that (note: Servo contains a reference of Network):
Exception ignored in: <bound method Servo.__del__ of <ingenialink.Servo object at 0x7fb262c83f28>>
Traceback (most recent call last):
File "/home/int.ingeniamc.com/gmarull/ws/ingenia/libs/ingenialink-python/ingenialink/__init__.py", line 498, in __del__
AttributeError: 'NoneType' object has no attribute 'il_servo_destroy'
Exception ignored in: <bound method Network.__del__ of <ingenialink.Network object at 0x7fb262c83ef0>>
Traceback (most recent call last):
File "/home/int.ingeniamc.com/gmarull/ws/ingenia/libs/ingenialink-python/ingenialink/__init__.py", line 384, in __del__
AttributeError: 'NoneType' object has no attribute 'il_net_destroy'
Where I understand that "lib" is NoneType. In some other cases I also get (after net/servo destructors have been called):
*** Error in `python3': corrupted double-linked list: 0x0000000002dfe6d0 ***
======= Backtrace: =========
/lib/x86_64-linux-gnu/libc.so.6(+0x777e5)[0x7f9987aed7e5]
/lib/x86_64-linux-gnu/libc.so.6(+0x80c71)[0x7f9987af6c71]
/lib/x86_64-linux-gnu/libc.so.6(cfree+0x4c)[0x7f9987afa53c]
python3[0x559c15]
python3[0x50d1cd]
python3(_PyGC_CollectNoFail+0x27)[0x600e17]
python3(PyImport_Cleanup+0x354)[0x51af74]
python3(Py_Finalize+0x5e)[0x602e1e]
python3(Py_Exit+0x8)[0x602f18]
python3[0x60300a]
python3(PyErr_PrintEx+0x36)[0x603076]
python3(PyRun_SimpleFileExFlags+0x1d9)[0x603d39]
python3(Py_Main+0x456)[0x63e756]
python3(main+0xe1)[0x4cfbd1]
/lib/x86_64-linux-gnu/libc.so.6(__libc_start_main+0xf0)[0x7f9987a96830]
python3(_start+0x29)[0x5d46c9]
Any clue on this issue? Am I doing something wrong, or could it be a cffi bug?