example: fixes deallocate error in Rust (#2327)

Signed-off-by: Takeshi Yoneda <t.y.mathetake@gmail.com>
This commit is contained in:
Takeshi Yoneda
2024-10-02 10:36:21 -07:00
committed by GitHub
parent 178eefe8b0
commit 51aba3703d
3 changed files with 7 additions and 2 deletions

View File

@@ -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 {

View File

@@ -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

Binary file not shown.