Stream: compiler development

Topic: echo memory leak


view this post on Zulip Anton (Dec 02 2025 at 18:33):

This "fixes" the memory leak in the echo example of Luke's zig platform but I think it may be a bad idea @Richard Feldman @Luke Boswell

diff --git a/platform/host.zig b/platform/host.zig
index fa18c29..502236a 100644
--- a/platform/host.zig
+++ b/platform/host.zig
@@ -206,21 +206,9 @@ fn hostedStdinLine(ops: *builtins.host_abi.RocOps, ret_ptr: *anyopaque, args_ptr
         line = line[0 .. line.len - 1];
     }

-    // Allocate through Roc's allocation system to ensure proper size-tracking metadata
-    var roc_alloc_args = builtins.host_abi.RocAlloc{
-        .alignment = 1,
-        .length = line.len,
-        .answer = undefined,
-    };
-    ops.roc_alloc(&roc_alloc_args, ops.env);
-
-    // Copy line data to the Roc-allocated memory
-    const line_copy: [*]u8 = @ptrCast(roc_alloc_args.answer);
-    @memcpy(line_copy[0..line.len], line);
-
     // Create RocStr from the read line and return it
     const result: *RocStr = @ptrCast(@alignCast(ret_ptr));
-    result.* = RocStr.init(line_copy, line.len, ops);
+    result.* = RocStr.fromSlice(line, ops);
 }

 /// Hosted function: Stdout.line! (index 2 - sorted alphabetically)

From https://github.com/Anton-4/roc-platform-template-zig/pull/2

view this post on Zulip Richard Feldman (Dec 02 2025 at 18:36):

oh yeah that sounds scary :joy:

view this post on Zulip Richard Feldman (Dec 02 2025 at 18:36):

pretty sure there's UB there haha

view this post on Zulip Richard Feldman (Dec 02 2025 at 18:58):

I wonder if the bug there is that we're not decref-ing tag union payloads correctly

view this post on Zulip Richard Feldman (Dec 02 2025 at 18:58):

like because the Str is inside a Try, when we decref the Try it doesn't go in and decref (and free) the Str inside the Ok?

view this post on Zulip Anton (Dec 03 2025 at 10:05):

Luke fixed it: https://github.com/lukewilliamboswell/roc-platform-template-zig/commit/b68e3f427d51acb5d2e76425f6e0fb8b21bdedfc


Last updated: Dec 21 2025 at 12:15 UTC