example: fixes deallocate error in Rust (#2327)
Signed-off-by: Takeshi Yoneda <t.y.mathetake@gmail.com>
This commit is contained in:
@@ -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 {
|
||||
|
||||
2
examples/allocation/rust/testdata/greet.rs
vendored
2
examples/allocation/rust/testdata/greet.rs
vendored
@@ -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
|
||||
|
||||
BIN
examples/allocation/rust/testdata/greet.wasm
vendored
BIN
examples/allocation/rust/testdata/greet.wasm
vendored
Binary file not shown.
Reference in New Issue
Block a user