I have structure in C++ as below
I am trying to create a object for this structure through CFFI and assign the values for the members and pass the created object to a API in C++.
Now when i try to pass this deltaUser to an API in C++ through the dll loaded using ffi.dlopenApiName(par1,par2,deltaUser,par4) python crashes at this point. crash occurs both in windows and Linux. What is wrong with this code ? I am using windows 7 64 bit , python2.7 and cffi 1.5.2 On debugging I could able to find that that the values passed gets optimized before hitting the API. On checking the python CFFI source codes I can able to find that padding/8 bit alignment is done in some part of codes. Is this a bug in CFFI or how can i overcome this problem form my script/c++ code side? Thanks in advance. |
I haven't looked at the code you attached, so I could be mistaken, but it looks like here:
deltaUser.deltaUser = ffi.new("CUser*")
Here you're creating a reference and storing it in a variable, but there are no references in a python variable so it get's garbage collected.
CUser* CreatePointerCUser(){
CUser* pCUser = ( CUser* ) malloc( sizeof( CUser ) );
_SetDummyValues( pCUser ); // That function sets attribute to 0 and NULL depending on their type.
return pCUser;
}
void FreeCUser( CUser* pCUser )
{
pCUser->id = 0; // That line...
free( pCUser->name );
pCUser->name = NULL; // And that line may look like overkilling it but at least your memory will be set to 0 values
}
gc_pCUser = ffi.gc( c.CreateCUser(), c.FreeCUser )
gc_pCUser = ffi.gc( c.CreatePointerCUser(), c.FreeCUser )
void FreeCUser( CUser* pCUser )
{
if( pCUser != NULL )
{
if( pCUser->name != NULL )
{
free( pCUser->name );
pCUser->name = NULL;
}
pCUser->id = 0;
... // other free and reset for the members
free( pCUser );
pCUser = NULL;
}
}