1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

builtin: free all elements in a []string with -autofree too

This commit is contained in:
Delyan Angelov 2021-03-18 21:10:42 +02:00
parent d4e16b6ddd
commit 00651c8deb
No known key found for this signature in database
GPG Key ID: 66886C0F12D595ED
2 changed files with 16 additions and 0 deletions

View File

@ -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 {

View File

@ -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
}