From 6d47dd22dfb7704e52064b81ed753da08aeb988b Mon Sep 17 00:00:00 2001 From: Alexander Medvednikov Date: Sat, 18 Sep 2021 04:19:16 +0300 Subject: [PATCH] autofree: free references --- vlib/v/gen/c/cgen.v | 6 +++++- vlib/v/tests/valgrind/2.heap_objects.v | 13 +++++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) create mode 100644 vlib/v/tests/valgrind/2.heap_objects.v diff --git a/vlib/v/gen/c/cgen.v b/vlib/v/gen/c/cgen.v index 3be54cecd9..8b41e4d237 100644 --- a/vlib/v/gen/c/cgen.v +++ b/vlib/v/gen/c/cgen.v @@ -3204,7 +3204,9 @@ fn (mut g Gen) autofree_scope_vars2(scope &ast.Scope, start_pos int, end_pos int fn (mut g Gen) autofree_variable(v ast.Var) { sym := g.table.get_type_symbol(v.typ) // if v.name.contains('output2') { - // eprintln(' > var name: ${v.name:-20s} | is_arg: ${v.is_arg.str():6} | var type: ${int(v.typ):8} | type_name: ${sym.name:-33s}') + if g.is_autofree { + // eprintln(' > var name: ${v.name:-20s} | is_arg: ${v.is_arg.str():6} | var type: ${int(v.typ):8} | type_name: ${sym.name:-33s}') + } // } if sym.kind == .array { if sym.has_method('free') { @@ -3241,6 +3243,8 @@ fn (mut g Gen) autofree_variable(v ast.Var) { } if sym.has_method('free') { g.autofree_var_call(c_name(sym.name) + '_free', v) + } else if v.typ.is_real_pointer() { + g.autofree_var_call('free', v) } } diff --git a/vlib/v/tests/valgrind/2.heap_objects.v b/vlib/v/tests/valgrind/2.heap_objects.v new file mode 100644 index 0000000000..5092e232ce --- /dev/null +++ b/vlib/v/tests/valgrind/2.heap_objects.v @@ -0,0 +1,13 @@ +struct User { + name string + age int +} + +fn simple() { + user := &User{'Bob', 31} + println('done') +} + +fn main() { + simple() +}