Version française
Home     About     Download     Resources     Contact us    
Browse thread
[Caml-list] GC, Anonymous Functions, and C
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: David Brown <caml-list@d...>
Subject: Re: [Caml-list] GC, Anonymous Functions, and C
On Sat, Dec 18, 2004 at 10:55:23AM +1300, Jonathan Roewen wrote:

> When passing an anonymous ocaml function to a C function, is it safe
> to store that value in an array, and then call it later at an abitrary
> time, or will the GC reclaim it? And if so, what should be done to
> stop the GC from doing so?

You'll need to register the value as a global root, using
register_global_root.  Please see the Interfacing C with Objective Caml,
where there is brief mention of this function.  It is also important to
dereference the value each time you use it.

static value hook = Val_int (0);

  ...
  hook = passed_argument;
  register_global_root (&hook);

  ...
  callback (hook, arg);

You can also use remove_global_root() later to remove the reference.

Dave