Skip to content

Commit 00651c8

Browse files
committed
builtin: free all elements in a []string with -autofree too
1 parent d4e16b6 commit 00651c8

File tree

2 files changed

+16
-0
lines changed

2 files changed

+16
-0
lines changed

vlib/builtin/array.v

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -485,6 +485,17 @@ pub fn (a &array) free() {
485485
C.free(a.data)
486486
}
487487

488+
[unsafe]
489+
pub fn (mut a []string) free() {
490+
$if prealloc {
491+
return
492+
}
493+
for s in a {
494+
unsafe { s.free() }
495+
}
496+
C.free(a.data)
497+
}
498+
488499
// str returns a string representation of the array of strings
489500
// => '["a", "b", "c"]'.
490501
pub fn (a []string) str() string {

vlib/v/gen/c/cgen.v

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2642,6 +2642,11 @@ fn (mut g Gen) autofree_variable(v ast.Var) {
26422642
// eprintln(' > var name: ${v.name:-20s} | is_arg: ${v.is_arg.str():6} | var type: ${int(v.typ):8} | type_name: ${sym.name:-33s}')
26432643
// }
26442644
if sym.kind == .array {
2645+
if sym.has_method('free') {
2646+
free_method_name := g.typ(v.typ) + '_free'
2647+
g.autofree_var_call(free_method_name, v)
2648+
return
2649+
}
26452650
g.autofree_var_call('array_free', v)
26462651
return
26472652
}

0 commit comments

Comments
 (0)