Skip to content

Commit

Permalink
example: fixes deallocate error in Rust (#2327)
Browse files Browse the repository at this point in the history
Signed-off-by: Takeshi Yoneda <t.y.mathetake@gmail.com>
  • Loading branch information
mathetake authored Oct 2, 2024
1 parent 178eefe commit 51aba37
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 2 deletions.
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.

0 comments on commit 51aba37

Please sign in to comment.