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

example: fixes deallocate error in Rust #2327

Merged
merged 1 commit into from
Oct 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion examples/allocation/rust/greet.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,12 @@ func main() {
greetingSize := uint32(ptrSize[0])
// This pointer was allocated by Rust, but owned by Go, So, we have to
// deallocate it when finished
defer deallocate.Call(ctx, uint64(greetingPtr), uint64(greetingSize))
defer func() {
_, err = deallocate.Call(ctx, uint64(greetingPtr), uint64(greetingSize))
if err != nil {
log.Panicln(err)
}
}()

// The pointer is a linear memory offset, which is where we write the name.
if bytes, ok := mod.Memory().Read(greetingPtr, greetingSize); !ok {
Expand Down
2 changes: 1 addition & 1 deletion examples/allocation/rust/testdata/greet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ pub extern "C" fn _allocate(size: u32) -> *mut u8 {
/// Allocates size bytes and leaks the pointer where they start.
fn allocate(size: usize) -> *mut u8 {
// Allocate the amount of bytes needed.
let vec: Vec<MaybeUninit<u8>> = Vec::with_capacity(size);
let vec: Vec<MaybeUninit<u8>> = vec![MaybeUninit::uninit(); size];

// into_raw leaks the memory to the caller.
Box::into_raw(vec.into_boxed_slice()) as *mut u8
Expand Down
Binary file modified examples/allocation/rust/testdata/greet.wasm
Binary file not shown.
Loading