This is the mail archive of the
libffi-discuss@sourceware.org
mailing list for the libffi project.
shallow copying in ffi_prep_cif
- From: Tom Dalling <tom dot dalling at gmail dot com>
- To: libffi-discuss at sourceware dot org
- Date: Tue, 30 Mar 2010 23:07:58 +1000
- Subject: shallow copying in ffi_prep_cif
Hi everyone,
I was using the libffi closure API roughly like this:
ffi_cif cif;
ffi_type* arg_types[1];
ffi_closure* closure;
arg_types[0] = &ffi_type_pointer;
ffi_prep_cif(&cif, FFI_DEFAULT_ABI, 1, &ffi_type_void, arg_types);
closure = ffi_closure_alloc(...);
ffi_prep_closure_loc(closure, &cif, ...);
I was then storing "closure" so that it could be called later on. However, when I tried to call "closure" I got a crash. Looking in the debugger, I found that "closure" has a pointer to the stack allocated "cif". Also, "cif" had a pointer to the stack allocated "arg_types". I malloced "cif" and "arg_types" instead of making them local, and everything worked fine.
Is this is the intended behaviour? I just assumed that ffi_prep_cif and ffi_prep_closure_loc would do a deep copy.
Also, when it comes to freeing the memory, is there anything wrong with doing the following?
free(closure->cif->arg_types);
free(closure->cif);
ffi_closure_free(closure);
Thanks,
Tom