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

Pass non-primitive types to C as 'borrowed' where appropriate #29

Merged
merged 1 commit into from
Sep 16, 2024

Conversation

paulcadman
Copy link
Owner

@paulcadman paulcadman commented Sep 16, 2024

Parameters passed to C functions are owned by the receiving function. As such, the receiving function must decrement the parameter's reference count to avoid memory leaks.

An alternative is to pass parameters as borrowed by using the @& prefix in their type. The receiving function does not need to decrement the parameter's reference count but it must only be passed to other non-consuming functions. Return values are owned by the caller, so parameters passed back to the caller should not be marked borrowed.

This change remedies a major memory leak in the orbital example (it calls drawLineV many times).

https://lean-lang.org/lean4/doc/dev/ffi.html#borrowing

Parameters passed to C functions are considered to be owned by the
receiving function. As such, the receiving function must decrement the
parameter's reference count to avoid memory leaks.

An alternative is to pass parameters as borrowed by using the `@&`
prefix in their type. The receiving function does not need to decrement
the parameter's reference count but it must only be passed to other
non-consuming functions.
@paulcadman paulcadman merged commit b1f1c5f into main Sep 16, 2024
2 checks passed
@paulcadman paulcadman deleted the ffi-borrowed-parameters branch September 16, 2024 12:39
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

Successfully merging this pull request may close these issues.

1 participant