V: for loop error

Created on 29 Jun 2020  路  15Comments  路  Source: vlang/v

V version: 0.1.28.1
OS: linux

What did you do?

m := {'a': 'b', 'c': 'd'}
mut arr := []string{}
for k, _ in m {
    arr << k
}
println(arr)

What did you expect to see?

['a', 'c']

What did you see instead?

['
Bug

Most helpful comment

I think I have a working solution for the `for k,v in map {} case only.

All 15 comments

m := {'a': 'b', 'c': 'd'}
mut arr := []string{}
for k, _ in m {
    arr << k.clone()
}
println(arr)

It can work.

I'm seeing the same issue too; moreover on Windows, this code example outputs:
['P', 'c']

I'm sure this is a regression. It worked on a V 0.1.28 of some days ago!

I can confirm that is a regression. It works in 7be9526 and does:

0[10:34:06]delian@nemesis: /ssd2/opt/v_linux/v $ /dev/shm/v_at_7be9526/v run /v/vvvv/aa.v
['a', 'c']

I am still bisecting where it regressed.

It regressed in 1848eb09 :
regression commit

There's a real problem in this, and this PR exposes the problem because the memory has been freed.

I agree. The keys array is freed after the loop, but the values are used in the << operation.

Yesterday, there was another bug that was fixed by doing g.optionals << styp.clone() too. I think the fix should be in the implementation of <<

@yuyi98 why did you close https://github.com/vlang/v/pull/5592 ? I think you were on the right track to solving it. Was there another problem?

I add array_clone in it, but memory leak.

with -autofree ?

no -autofree, ci test

I don't know how to solve this problem. Is it resolved in the parser stage?

hm, you are right; doing an implicit clone will need much more work and analysis

I think I have a working solution for the `for k,v in map {} case only.

Fixed in ca1f47a :
for k,v in map{} fix

Was this page helpful?
0 / 5 - 0 ratings

Related issues

penguindark picture penguindark  路  3Comments

markgraydev picture markgraydev  路  3Comments

ArcDrake picture ArcDrake  路  3Comments

vtereshkov picture vtereshkov  路  3Comments

radare picture radare  路  3Comments