Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Document that ocaml values need to be rooted #134

Open
Lupus opened this issue May 28, 2023 · 3 comments
Open

Document that ocaml values need to be rooted #134

Lupus opened this issue May 28, 2023 · 3 comments

Comments

@Lupus
Copy link

Lupus commented May 28, 2023

Maybe I've missed this in the documentation, but I didn't realize that storing ocaml::Value somewhere outside of #[ocaml::func] will lead to crashes after OCaml GC disposes the value.

After adding this to my function, things started to work:

let handler = ocaml::Value::from(handler.raw().0);

Perhaps ocaml::Value should be provided with custom Clone implementation that ensures that cloned value is properly rooted.

@Lupus
Copy link
Author

Lupus commented Jun 1, 2023

Maybe some helper like this needs to be added to the library and documented? 🤔

pub fn ensure_rooted_value(value: ocaml::Value) -> ocaml::Value {
    match value {
        ocaml::Value::Raw(v) => unsafe { ocaml::Value::Root(ocaml::root::Root::new(v)) },
        ocaml::Value::Root(_) => value,
    }
}

@zshipko
Copy link
Owner

zshipko commented Jun 1, 2023

Adding something like that seems reasonable to me. Could you open a PR for this?

@Lupus
Copy link
Author

Lupus commented Jun 1, 2023

Yeah, sure. Will do.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants