public inbox for libffi-discuss@sourceware.org
 help / color / mirror / Atom feed
* Re: shallow copying in ffi_prep_cif
       [not found] <1792165289.62557.1269966359890.JavaMail.root@cm-mail03.mozilla.org>
@ 2010-03-30 16:32 ` Dan Witte
  2010-03-30 16:51   ` Tom Dalling
  0 siblings, 1 reply; 4+ messages in thread
From: Dan Witte @ 2010-03-30 16:32 UTC (permalink / raw)
  To: Tom Dalling; +Cc: libffi-discuss

----- "Tom Dalling" <tom.dalling@gmail.com> wrote:
> Is this is the intended behaviour? I just assumed that ffi_prep_cif
> and ffi_prep_closure_loc would do a deep copy.

Yeah, you need to keep them both alive for the lifetime of the closure. I remember reading about this in the README, or the info page, or something -- does it not specify?

> 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);

Nope, assuming you malloc'ed closure->cif->arg_types and closure->cif. :)

Note that you would also need to free any FFI_TYPE_STRUCT ffi_types that you created.

Dan

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: shallow copying in ffi_prep_cif
  2010-03-30 16:32 ` shallow copying in ffi_prep_cif Dan Witte
@ 2010-03-30 16:51   ` Tom Dalling
  2010-03-30 17:02     ` Dan Witte
  0 siblings, 1 reply; 4+ messages in thread
From: Tom Dalling @ 2010-03-30 16:51 UTC (permalink / raw)
  To: libffi-discuss


On 31/03/2010, at 2:31 AM, Dan Witte wrote:
> ----- "Tom Dalling" <tom.dalling@gmail.com> wrote:
>> Is this is the intended behaviour? I just assumed that ffi_prep_cif
>> and ffi_prep_closure_loc would do a deep copy.
> 
> Yeah, you need to keep them both alive for the lifetime of the closure. I remember reading about this in the README, or the info page, or something -- does it not specify?

Thanks for your help. I had a look through the README and info pages again, but I don't think it gets a mention. The info page actually has an example that uses stack allocated variables, which is how I got tripped up.

--Tom






^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: shallow copying in ffi_prep_cif
  2010-03-30 16:51   ` Tom Dalling
@ 2010-03-30 17:02     ` Dan Witte
  0 siblings, 0 replies; 4+ messages in thread
From: Dan Witte @ 2010-03-30 17:02 UTC (permalink / raw)
  To: Tom Dalling; +Cc: libffi-discuss

----- "Tom Dalling" <tom.dalling@gmail.com> wrote:

> On 31/03/2010, at 2:31 AM, Dan Witte wrote:
> > ----- "Tom Dalling" <tom.dalling@gmail.com> wrote:
> >> Is this is the intended behaviour? I just assumed that
> ffi_prep_cif
> >> and ffi_prep_closure_loc would do a deep copy.
> > 
> > Yeah, you need to keep them both alive for the lifetime of the
> closure. I remember reading about this in the README, or the info
> page, or something -- does it not specify?
> 
> Thanks for your help. I had a look through the README and info pages
> again, but I don't think it gets a mention. The info page actually has
> an example that uses stack allocated variables, which is how I got
> tripped up.

Mmm, you're right. I think I figured out the lifetime bit by reading closures.c itself.

If you want to write a patch to update the docs appropriately, I'm sure Anthony would take it :)

Dan.

^ permalink raw reply	[flat|nested] 4+ messages in thread

* shallow copying in ffi_prep_cif
@ 2010-03-30 13:08 Tom Dalling
  0 siblings, 0 replies; 4+ messages in thread
From: Tom Dalling @ 2010-03-30 13:08 UTC (permalink / raw)
  To: libffi-discuss

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






^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2010-03-30 17:02 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
     [not found] <1792165289.62557.1269966359890.JavaMail.root@cm-mail03.mozilla.org>
2010-03-30 16:32 ` shallow copying in ffi_prep_cif Dan Witte
2010-03-30 16:51   ` Tom Dalling
2010-03-30 17:02     ` Dan Witte
2010-03-30 13:08 Tom Dalling

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).