From 8d8907b61eaae2631e206effde90b9dd1262aa0b Mon Sep 17 00:00:00 2001 From: Alexander Medvednikov Date: Sun, 22 Mar 2020 13:40:53 +0100 Subject: [PATCH] memory: handle string assignments --- vlib/v/gen/cgen.v | 6 ++++++ vlib/v/tests/valgrind/1.vv | 2 +- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/vlib/v/gen/cgen.v b/vlib/v/gen/cgen.v index 409e7699b8..b66efc1129 100644 --- a/vlib/v/gen/cgen.v +++ b/vlib/v/gen/cgen.v @@ -512,6 +512,12 @@ fn (g mut Gen) gen_assign_stmt(assign_stmt ast.AssignStmt) { g.expr(val) g.write(')') } + else if g.autofree && right_sym.kind == .string && is_ident { + // `str1 = str2` => `str1 = str2.clone()` + g.write(' = string_clone(') + g.expr(val) + g.write(')') + } else if !is_fixed_array_init { g.write(' = ') if !is_decl { diff --git a/vlib/v/tests/valgrind/1.vv b/vlib/v/tests/valgrind/1.vv index c90973b34e..c4e73b9a14 100644 --- a/vlib/v/tests/valgrind/1.vv +++ b/vlib/v/tests/valgrind/1.vv @@ -10,7 +10,7 @@ fn foo() { nums_copy := nums // array assignments call .clone() println(nums) println(nums_copy) - nums.free() + // nums.free() // this should result in a double free and a CI error } fn main() {