memory-leakszig

zig allocators dont need dealloc?


I have been using zig some days and start to learn about allocators. By what I know, zig doesn't have garbage collector, so something like the following code must bring an error:

const std = @import ("std");
const alloc_1 = std.heap.page_allocator;

pub fn main () !void {
    const arr = try alloc_1.alloc (u8, 3);
    arr[0] = 0;
}

I have using valgrind to track the memory leak with the next command valgrind --leak-check=full --track-origins=yes ./allocators and get this:

==23890== Memcheck, a memory error detector
==23890== Copyright (C) 2002-2022, and GNU GPL'd, by Julian Seward et al.
==23890== Using Valgrind-3.22.0 and LibVEX; rerun with -h for copyright info
==23890== Command: ./allocators
==23890== 
==23890== 
==23890== HEAP SUMMARY:
==23890==     in use at exit: 0 bytes in 0 blocks
==23890==   total heap usage: 0 allocs, 0 frees, 0 bytes allocated
==23890== 
==23890== All heap blocks were freed -- no leaks are possible
==23890== 
==23890== For lists of detected and suppressed errors, rerun with: -s
==23890== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)

so basically the memory is cleared despite I don't dealloc it.


Solution

  • page_allocator typically should not be used in zig. For every allocation, regardless of size, it allocates a full page of memory (typically 4kB or more) and requires a syscall.

    Valgrind does not detect allocations done from zig's page_allocator by default. To use valgrind, you have to link libc and use std.heap.c_allocator or configure it to support a different allocator. There is a proposal to have built-in support for other zig allocators, but it has not been implemented yet: #1837.

    Zig has its own leak-checking mechanism built-in. To get errors about unfreed memory, you can use zig's GeneralPurposeAllocator:

    const std = @import("std");
    
    pub fn main() !void {
        var gpa = std.heap.GeneralPurposeAllocator(.{}){};
        defer if (gpa.deinit() == .leak) std.os.exit(1);
        const alloc = gpa.allocator();
    
        const arr = try alloc.alloc (u8, 3);
        arr[0] = 0;
    }
    
    
    error(gpa): memory address 0x104568000 leaked: 
    a.zig:9:33: 0x10443c7b7 in main (a)
        const arr = try alloc.alloc (u8, 3);
                                    ^
    /.../lib/std/start.zig:583:37: 0x10443cf83 in main (a)
                const result = root.main() catch |err| {
                                        ^
    ???:?:?: 0x18d7dd0df in ??? (???)
    ???:?:?: 0x6017ffffffffffff in ??? (???)
    
    Exited with code [1]