Hey all, first time poster...
I'm working on a REPL that parses user commands. Came across a memory leak being reported by the tracking_allocator for when you use bufio.reader_read_string to read an empty line and then trim the resulting line getting a string of length 0.
I've extracted the code out of the application and still does the same:
main :: proc() {
default := context.allocator
tracking_allocator: mem.Tracking_Allocator
mem.tracking_allocator_init(&tracking_allocator, default)
defer mem.tracking_allocator_destroy(&tracking_allocator)
context.allocator = mem.tracking_allocator(&tracking_allocator)
defer print_memory_usage(&tracking_allocator)
r_buf: [1024]byte
reader: bufio.Reader
bufio.reader_init_with_buf(&reader, os.stream_from_handle(os.stdin), r_buf[:])
defer bufio.reader_destroy(&reader)
fmt.fprint(os.stdout, "> ")
line, r_err := bufio.reader_read_string(&reader, '\n', context.allocator)
if r_err != nil {
fmt.eprintf("Read Error: %s\n", r_err)
return
}
defer delete(line, context.allocator)
line = strings.trim_right(line, "\r")
line = strings.trim_space(line)
fmt.fprintf(os.stdout, "Echo: %s\n", line)
}
print_memory_usage :: proc(tracking_allocator: ^mem.Tracking_Allocator) {
if len(tracking_allocator.allocation_map) > 0 {
fmt.eprintln("Memory Leaks: ")
for _, entry in tracking_allocator.allocation_map {
fmt.eprintf(" - Leaked %d @ %v\n", entry.size, entry.location)
}
}
}
So if you just press enter at the prompt you get:
>
Echo:
Memory Leaks:
- Leaked 8 @ /path/to/odin/core/bufio/reader.odin(421:2)
Since this is a REPL won't that add up over time? Does 'delete' ignore empty lines or something?
Thanks