How to use os2.read_all_directory_by_path(...)? It leaks memory

Hi, I cannot figure out what to do to not leak memory using os2.read_all_directory_by_path

ExecuteEveryFrame :: proc(folderpath: string) {
    free_all(context.temp_allocator)
    context.allocator = context.temp_allocator
    fileinfos, err_dirread := os2.read_all_directory_by_path(folderpath, context.temp_allocator)
    os2.file_info_slice_delete(fileinfos,context.temp_allocator)
    this_gets_freeds_ok := make([dynamic]string,500000)
    for &e in this_gets_freeds_ok {
        e="asd"
    }
}

Can you help us help you by providing the output of odin report and your tracking/leak result/report?

I dont know how to do it
here is both, the minimal working example and the report results:

package main
import "core:strings"
import "core:os/os2"

YOUR_PATH :: "C:/Users"
main :: proc() {
    @static counter:int

    for  true{
        counter+=1
        if counter % 777 == 0 do ExecuteEveryFrame(YOUR_PATH)
        free_all(context.temp_allocator)
    }
}

ExecuteEveryFrame :: proc(folderpath: string) {
    context.allocator = context.temp_allocator
    fileinfos, err_dirread := os2.read_all_directory_by_path(folderpath, context.temp_allocator)
    // os2.file_info_slice_delete(fileinfos,context.temp_allocator) //doesnt help

    // this_gets_freeds_ok := make([dynamic]string,500000)
    // for &e in this_gets_freeds_ok {
    //     e="asd"
    // }
}

And the report:


        Odin:    dev-2024-09-nightly:dd1f151
        OS:      Windows 11 Unknown Edition (00000064) (version: 22H2), build 22621.4317
        CPU:     Intel(R) Core(TM) i5-7200U CPU @ 2.50GHz
        RAM:     16223 MiB
        Backend: LLVM 18.1.8

what is interesting if that if I uncomment the thing, the memory grows SLOWER, that might be a hint to the bug

//UNCOMMENTING THE FOLLOWING THE MEMORY GROWS SLWOER
// this_gets_freeds_ok := make([dynamic]string,500000)
    // for &e in this_gets_freeds_ok {
    //     e="asd"
    // }

the memory grows slower, I will just paste it uncommented, I also decreased the "frame limiter2 so be careful, dont uncomment free_all or your PC will lag instantly:

package main
import "core:strings"
import "core:os/os2"

YOUR_PATH :: "C:/Users"
main :: proc() {
    @static counter:int

    for  true{
        counter+=1
        if counter % 1 == 0 do ExecuteEveryFrame(YOUR_PATH)
        free_all(context.temp_allocator)
    }
}

ExecuteEveryFrame :: proc(folderpath: string) {
    context.allocator = context.temp_allocator
    fileinfos, err_dirread := os2.read_all_directory_by_path(folderpath, context.temp_allocator)
    // os2.file_info_slice_delete(fileinfos,context.temp_allocator) //doesnt help

    this_gets_freeds_ok := make([dynamic]string,500000)
    for &e in this_gets_freeds_ok {
        e="asd"
    }
}

I also noticed that the memory leak is almost independant of the frame limiter, it starts slowing down when I use count % 777 but I barely notice a dMEM/dt when I use 777 or 1

1 Like

Thank you, I have found the leak and fixed it along with some other issues in this PR: os2: fix leak in dir_windows, fix netbsd, and add a test for dir reading by laytan · Pull Request #4489 · odin-lang/Odin · GitHub

3 Likes