# Calling impure closures from C

**URL:** <https://discuss.ocaml.org/t/calling-impure-closures-from-c/6201>\
**Category:** Learning\
**Created:** [August 5, 2020, 8:50pm UTC](https://discuss.ocaml.org/t/calling-impure-closures-from-c/6201 "2020-08-05T20:50:41Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![zbaylin](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/zbaylin/32/4047_2.png) [@zbaylin](https://discuss.ocaml.org/u/zbaylin)\
**Post date:** [August 5, 2020, 8:50pm UTC](https://discuss.ocaml.org/t/calling-impure-closures-from-c/6201/1 "2020-08-05T20:50:41Z")

</div>

I’m using the FFI to write bindings from OCaml to various native C libraries (namely Cocoa). One problem I came across is that if a callback is impure, it uses the “most recent” data from the OCaml side.

For example, if I have a basic `for` loop and iterate from 1 to n, passing in the callback `fun () -> print_endline i` for each iteration, and I call the passed callbacks at the end, they all print n, rather than printing `1...n`.

Is there a solution to this in the manual that I’m missing? I’m worried that this type of thing would cause unintended behavior, especially when using something like `Array.init` and expecting the value of the index to always be up-to-date.

---

<div class="post-metadata">

**Author:** ![Chet\_Murthy](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/chet_murthy/32/1501_2.png) [@Chet\_Murthy](https://discuss.ocaml.org/u/Chet_Murthy)\
**Post date:** [August 5, 2020, 8:54pm UTC](https://discuss.ocaml.org/t/calling-impure-closures-from-c/6201/2 "2020-08-05T20:54:21Z")

</div>

[not a maintainer, just a curious bystander] Do you have a contained and buildable testcase that elicits the problem?

---

<div class="post-metadata">

**Author:** ![dbuenzli](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/dbuenzli/32/18_2.png) [@dbuenzli](https://discuss.ocaml.org/u/dbuenzli)\
**Post date:** [August 5, 2020, 9:12pm UTC](https://discuss.ocaml.org/t/calling-impure-closures-from-c/6201/3 "2020-08-05T21:12:24Z")

</div>

> [@zbaylin](#):
>
> For example, if I have a basic `for` loop and iterate from 1 to n, passing in the callback `fun () -> print_endline i` for each iteration, and I call the passed callbacks at the end, they all print n, rather than printing `1...n` .

It’s a bit unclear on which side the various part of this description are. As @Chet_Murthy mentions sample code (even if pseudo code) would help.

I suspect you may have captured the `i` of the `fun () -> print_endline i` in a closure, that unit `()` here looks suspicious :–)

---

<div class="post-metadata">

**Author:** ![Chet\_Murthy](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/chet_murthy/32/1501_2.png) [@Chet\_Murthy](https://discuss.ocaml.org/u/Chet_Murthy)\
**Post date:** [August 5, 2020, 9:14pm UTC](https://discuss.ocaml.org/t/calling-impure-closures-from-c/6201/4 "2020-08-05T21:14:43Z")

</div>

[Rank and idle speculation here] If this is happening with the bytecode runtime, I wonder if it’s related to the way that closures are allocated on-stack? It would explain the described behaviour. [geez, rank speculation]

---

<div class="post-metadata">

**Author:** ![zbaylin](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/zbaylin/32/4047_2.png) [@zbaylin](https://discuss.ocaml.org/u/zbaylin)\
**Post date:** [August 5, 2020, 9:25pm UTC](https://discuss.ocaml.org/t/calling-impure-closures-from-c/6201/5 "2020-08-05T21:25:59Z")

</div>

This is actually part of the Revery GUI project, example code can be found here:

> <https://github.com/revery-ui/revery/blob/a9f19412f83a0d0ffdcac202c2f03d32b0884a18/examples/MenuExample.re#L25>

  
The callbacks are called later (on the NSMenuItems being clicked) here:  

> <https://github.com/revery-ui/revery/blob/a9f19412f83a0d0ffdcac202c2f03d32b0884a18/src/Native/cocoa/ReveryMenuHandler.c#L28>

If you run this, the example app always prints “Item #9 clicked” rather than the actual value of `i` at the time.

Forgive the various levels of indirection – the callbacks are obviously not called immediately and have to be stored.

---

<div class="post-metadata">

**Author:** ![dbuenzli](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/dbuenzli/32/18_2.png) [@dbuenzli](https://discuss.ocaml.org/u/dbuenzli)\
**Post date:** [August 5, 2020, 9:41pm UTC](https://discuss.ocaml.org/t/calling-impure-closures-from-c/6201/6 "2020-08-05T21:41:19Z")

</div>

> [@zbaylin](#):
>
> ` revery_caml_register_global(globalCallback);`

After going through the various definitions, I think you are registering the wrong thing here. You should register `camlCallback`.
