Skip to content

How do I free memory? #205

Discussion options

You must be logged in to vote
  1. Can C code free(result.ptr) hoping that allocator is the same across FFI boundary?

    The short answer is no: the allocator machinery/implementation that Rust uses for things such as Boxes, Vecs, etc. (i.e., the registered impl GlobalAlloc), can be fully independent of libc/posix malloc/free, and thus free() may not be the appropriate memory-releasing operation (and UB could ensue!).

    The longer, and more hopeful answer, is that, when compiling a Rust binary, be it a [bin]ary standalone executable, or a "cdylib", "staticlib" "C" library, you can override the default behavior of Rust allocations via the #[global_allocator] directive. By doing so, you can then specify that all allocations g…

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@rlidwka
Comment options

Answer selected by danielhenrymantilla
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
2 participants