Python Cffi Memory Management Issues
I am programming on Ubuntu, with Python 2.7.3. I am using CFFI to populate a Python list with values coming from some C code. That list is quite big : around 71 000 characters long
Solution 1:
I found how to fix my issues:
I used ffi.gc(cdata, destructor)
to create the structure. My Python code now looks like:
data_list = []
for i in range( 0, x ):
# Create a pointer to the data structure and tell the garbage collector how to destroy it
gc_c_pDataStructure = ffi.gc( c.CreateDataStructure(), c.FreeDataStructure )
c.SomeCFunction( gc_c_pDataStructure ) # Populate the data structure
datas_list.append( c.GetSomeInfo( gc_c_pDataStructure ) ) # Store some data
Here are some links related to ffi.gc()
:
And here is the C function to create the data structure (according to the structure example from the question):
PSTRUCT1 CreateDataStructure()
{
PSTRUCT1 pStruct1 = ( PSTRUCT1 ) malloc( sizeof( STRUCT1 ) );
_SetDummyValues( pStruct1 );
return pStruct1;
}
As you can see, I had to create the function void _SetDummyValues( PSTRUCT1 pStruct1 )
. That function sets the given structure pointers to NULL.
Solution 2:
This may have also been related to this bug which Coverity found in some of our CFFI-generated code:
x0 = (void *)alloca((size_t)datasize);
...
{
free(x0);
}
As you can see, it's calling free on stack-allocated memory.
Post a Comment for "Python Cffi Memory Management Issues"