Pass non-primitive types to C as 'borrowed' where appropriate #29
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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