zig allocators dont need dealloc?

126 Views Asked by At

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.

2

There are 2 best solutions below

0
Tunify Basic On

i don't think that valgrind works with llvm but idk you may prefer doing something like

const memory = try allocator.alloc(u8, 100);
defer allocator.free(memory);

ref: https://zig.guide/standard-library/allocators/

0
pfg On

Zig allocators need do need dealloc, but valgrind doesn't work with custom allocators

To get errors about unfreed memory, you can use zig's GeneralPurposeAllocator:

const std = @import ("std");
var gpa = std.heap.GeneralPurposeAllocator(.{}){};

pub fn main () !void {
    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]

You may also be able to use std.heap.c_allocator by adding exe.linkLibC() in build.zig or -lc to zig build-exe / zig run along with valgrind, but I have not tested this.