Skip to content

Trivial program puts variable on heap unnecessarily, autofree does not free it #14913

@justinas

Description

@justinas

V version: V 0.3.0 ec75860

OS: linux, Linux version 5.15.46 (nixbld@localhost) (gcc (GCC) 11.3.0, GNU ld (GNU Binutils) 2.38) #1-NixOS SMP Thu Jun 9 08:23:32 UTC 2022

What did you do?

 $ echo "fn main() {
    for i := 0; i < 1000; i++ {
        a := 42
        ref := &a
        println(ref)
    }
}
" > sandbox.v
$ ./v -autofree -gc none -prod sandbox.v
$ valgrind ./sandbox
<...>
==5076== HEAP SUMMARY:
==5076==     in use at exit: 17,000 bytes in 2,000 blocks
==5076==   total heap usage: 6,019 allocs, 4,019 frees, 302,264 bytes allocated
==5076==
==5076== LEAK SUMMARY:
==5076==    definitely lost: 17,000 bytes in 2,000 blocks

What did you expect to see?

No leaks.

What did you see instead?

Leaks reported by Valgrind.

One can also adjust the loop iterations and see it leaking proportionally more. Or make the loop infinite and see the program slowly eat up all the memory.

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugThis tag is applied to issues which reports bugs.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions