fix: clean up all memory leaks. use tracking allocator in example

This commit is contained in:
2025-05-25 22:44:14 +02:00
parent 8982186b72
commit 3175b3a23b
3 changed files with 48 additions and 17 deletions
+39 -4
View File
@@ -5,20 +5,24 @@ import "core:image"
import "core:image/qoi"
import "core:image/png"
import "core:slice"
import "core:mem"
import "core:os/os2"
import blurhash "../"
decode_example :: proc() {
if len(os2.args) < 3 {
fmt.eprintln("Please the blurhash as an arg")
fmt.eprintln("Please input the blurhash as an arg")
return
}
track: mem.Tracking_Allocator
mem.tracking_allocator_init(&track, context.allocator)
alloc := mem.tracking_allocator(&track)
width := 400
height := 240
pixels, err := blurhash.decode(os2.args[2], width, height, 1.2)
defer delete(pixels)
pixels, err := blurhash.decode(os2.args[2], width, height, 1.2, alloc)
if err != nil {
fmt.eprintln(err)
return
@@ -35,6 +39,20 @@ decode_example :: proc() {
fmt.eprintln(save_err)
return
}
delete(pixels, alloc)
for _, leak in track.allocation_map {
fmt.printfln("%v leaked %m", leak.location, leak.size)
}
for bad_free in track.bad_free_array {
fmt.printfln("%v allocation %p was freed badly", bad_free.location, bad_free.memory)
}
fmt.printfln("Peak Mem: %m", track.peak_memory_allocated)
fmt.printfln("Total Mem: %m", track.total_memory_allocated)
fmt.printfln("Current Mem: %m", track.current_memory_allocated)
fmt.printfln("Total Allocs: %v, Total Frees: %v:", track.total_allocation_count, track.total_free_count)
fmt.printfln("Total Mem Free'd: %m", track.total_memory_freed)
}
encode_example :: proc() {
@@ -49,13 +67,30 @@ encode_example :: proc() {
return
}
bhash, hash_err := blurhash.encode(img.pixels.buf[:], 5, 4, img.width, img.height, img.width*img.channels)
track: mem.Tracking_Allocator
mem.tracking_allocator_init(&track, context.allocator)
alloc := mem.tracking_allocator(&track)
bhash, hash_err := blurhash.encode(img.pixels.buf[:], 5, 4, img.width, img.height, img.width*img.channels, alloc)
if hash_err != nil {
fmt.eprintln(hash_err)
return
}
fmt.println(bhash)
delete(bhash, alloc)
for _, leak in track.allocation_map {
fmt.printfln("%v leaked %m", leak.location, leak.size)
}
for bad_free in track.bad_free_array {
fmt.printfln("%v allocation %p was freed badly", bad_free.location, bad_free.memory)
}
fmt.printfln("Peak Mem: %m", track.peak_memory_allocated)
fmt.printfln("Total Mem: %m", track.total_memory_allocated)
fmt.printfln("Current Mem: %m", track.current_memory_allocated)
fmt.printfln("Total Allocs: %v, Total Frees: %v:", track.total_allocation_count, track.total_free_count)
fmt.printfln("Total Mem Free'd: %m", track.total_memory_freed)
}
main :: proc() {