diff --git a/vlib/v/tests/valgrind/free_heap_foos.v b/vlib/v/tests/valgrind/free_heap_foos.v new file mode 100644 index 0000000000..2d7971bd28 --- /dev/null +++ b/vlib/v/tests/valgrind/free_heap_foos.v @@ -0,0 +1,50 @@ +[has_globals] +module main + +__global frees = []int{cap: 100} + +struct Foo { + x int +} + +fn create(x int) &Foo { + res := &Foo{x} + println('> creating Foo $res.x at address: ${voidptr(res)}') + return res +} + +fn (f &Foo) free() { + println('> freeing Foo $f.x at address: ${voidptr(f)} | frees.len: $frees.len') + frees << f.x + // TODO: this should NOT be necessary - the compiler should do it automatically in the parent scope + unsafe { free(f) } +} + +fn create_some_foos() { + starting := frees.len + a := create(111) + assert frees.len == starting + b := create(222) + assert frees.len == starting + println(' > create_some_foos a: $a.x') + println(' > create_some_foos b: $b.x') + assert frees.len == starting +} + +fn main() { + create_some_foos() + if C._VAUTOFREE == 1 { + assert frees.len == 2 + assert frees[0] == 222 + assert frees[1] == 111 + } + create_some_foos() + if C._VAUTOFREE == 1 { + assert frees.len == 4 + assert frees[0] == 222 + assert frees[1] == 111 + assert frees[2] == 222 + assert frees[3] == 111 + } + unsafe { frees.free() } +}