diff --git a/vlib/builtin/array.v b/vlib/builtin/array.v index d9d512416e..a8326ae6b7 100644 --- a/vlib/builtin/array.v +++ b/vlib/builtin/array.v @@ -485,6 +485,17 @@ pub fn (a &array) free() { C.free(a.data) } +[unsafe] +pub fn (mut a []string) free() { + $if prealloc { + return + } + for s in a { + unsafe { s.free() } + } + C.free(a.data) +} + // str returns a string representation of the array of strings // => '["a", "b", "c"]'. pub fn (a []string) str() string { diff --git a/vlib/v/gen/c/cgen.v b/vlib/v/gen/c/cgen.v index 1bf7bb5e24..cb333112d4 100644 --- a/vlib/v/gen/c/cgen.v +++ b/vlib/v/gen/c/cgen.v @@ -2642,6 +2642,11 @@ fn (mut g Gen) autofree_variable(v ast.Var) { // 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') { + free_method_name := g.typ(v.typ) + '_free' + g.autofree_var_call(free_method_name, v) + return + } g.autofree_var_call('array_free', v) return }