From ef817fb111f63f77035d78597ed08c52614a22b0 Mon Sep 17 00:00:00 2001 From: Kinfe123 Date: Sun, 29 Nov 2026 14:54:03 -0800 Subject: [PATCH] Free the buffer `maybe_pop_list` returns for a `bytes` value `maybe_pop_list` copies a `bytes` value into a buffer from `alloc::alloc` and returns it, and wit-dylib lowers the list straight from that buffer. wit-dylib only frees lists it lowers element by element, so this buffer belongs to the interpreter, which never freed it: every `bytes` value a component passed to an import as a `list` stayed allocated for the life of the instance. Register the buffer with `defer_deallocate`, like the runtime's other temporary buffers, so it's freed when the `MyCall` drops. A pending async import keeps its `MyCall` until the subtask finishes, and an export keeps its `MyCall` until post-return. Fixes #261. --- runtime/src/lib.rs | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/runtime/src/lib.rs b/runtime/src/lib.rs index 5d5a07b..b31ab91 100644 --- a/runtime/src/lib.rs +++ b/runtime/src/lib.rs @@ -1739,9 +1739,13 @@ impl Call for MyCall<'_> { let src = self.stack.pop().unwrap(); let src = src.cast_bound::(py).unwrap(); let len = src.len().unwrap(); + let layout = Layout::from_size_align(len, 1).unwrap(); let dst = unsafe { - let dst = alloc::alloc(Layout::from_size_align(len, 1).unwrap()); + let dst = alloc::alloc(layout); slice::from_raw_parts_mut(dst, len).copy_from_slice(src.as_bytes()); + // The lowered list is only read during the call, so free + // it when the call ends. + self.defer_deallocate(dst, layout); dst }; Some((dst as _, len))