Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Memory leak with implicit result #21703

Closed
Gruruya opened this issue Apr 21, 2023 · 2 comments · Fixed by #21721
Closed

Memory leak with implicit result #21703

Gruruya opened this issue Apr 21, 2023 · 2 comments · Fixed by #21721
Assignees

Comments

@Gruruya
Copy link
Contributor

Gruruya commented Apr 21, 2023

Description

status-im/nim-stew#183

The implicit result causes a memory leak.

func toBytes*(s: string): seq[byte] =
  when nimvm:
    let r = newSeq[byte](4) # arbitrary
    r
  else:
    @(s.toOpenArrayByte(0, s.high))

discard "hello".toBytes

The explicit result does not.

func toBytes*(s: string): seq[byte] =
  when nimvm:
    let r = newSeq[byte](4)
    r
  else:
    result = @(s.toOpenArrayByte(0, s.high))

discard "hello".toBytes

Nim Version

1.9.3
c136ebf

Current Output

nim c --mm:orc -d:useMalloc test
valgrind --leak-check=full ./test

==29138== HEAP SUMMARY:
==29138==     in use at exit: 13 bytes in 1 blocks
==29138==   total heap usage: 7 allocs, 6 frees, 1,161 bytes allocated
==29138== 
==29138== 13 bytes in 1 blocks are definitely lost in loss record 1 of 1
==29138==    at 0x48455EF: calloc (vg_replace_malloc.c:1328)
==29138==    by 0x10AB1A: alloc0Impl__system_1770 (in /home/user/test)
==29138==    by 0x10AB44: allocShared0Impl__system_1783 (in /home/user/test)
==29138==    by 0x1104C2: alignedAlloc0__system_1948 (in /home/user/test)
==29138==    by 0x1107AB: newSeqPayload (in /home/user/test)
==29138==    by 0x110BBF: prepareSeqAdd (in /home/user/test)
==29138==    by 0x111108: setLen__test_12 (in /home/user/test)
==29138==    by 0x11126C: newSeq__test_4 (in /home/user/test)
==29138==    by 0x11130C: at___test_147 (in /home/user/test)
==29138==    by 0x1120C9: toBytes__test_1 (in /home/user/test)
==29138==    by 0x112235: NimMainModule (in /home/user/test)
==29138==    by 0x112154: NimMainInner (in /home/user/test)
==29138== 
==29138== LEAK SUMMARY:
==29138==    definitely lost: 13 bytes in 1 blocks
==29138==    indirectly lost: 0 bytes in 0 blocks
==29138==      possibly lost: 0 bytes in 0 blocks
==29138==    still reachable: 0 bytes in 0 blocks
==29138==         suppressed: 0 bytes in 0 blocks

Expected Output

==29099== HEAP SUMMARY:
==29099==     in use at exit: 0 bytes in 0 blocks
==29099==   total heap usage: 6 allocs, 6 frees, 1,148 bytes allocated
==29099== 
==29099== All heap blocks were freed -- no leaks are possible
==29099== 
==29099== For lists of detected and suppressed errors, rerun with: -s
==29099== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)

Possible Solution

No response

Additional Information

No response

@Gruruya
Copy link
Contributor Author

Gruruya commented Apr 21, 2023

The memleak doesn't occur in vm, to make that clear.

@Gruruya
Copy link
Contributor Author

Gruruya commented Apr 24, 2023

Here is a more minimized example

func example*(s: seq[int]): seq[int] =
  when nimvm:
    s
  else:
    @(s.toOpenArray(0, s.high))

discard @[1, 2, 3].example

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants