Skip to content

memory allocation during {.global.} init breaks GC #17085

Closed
@arnetheduck

Description

@arnetheduck
[arnetheduck@tempus tmp]$ more testit.nim
proc init(): string =
  for a in 0..<10000000:
    result.add 'c'

proc f() =
  var a {.global.} = init()
  var b {.global.} = init()
  var c {.global.} = init()

  echo a.len
  echo b.len
  echo c.len

f()
[arnetheduck@tempus tmp]$ nim c --hints:off -r testit.nim 
9920232
9920232
10000000
[arnetheduck@tempus tmp]$ nim --version
Nim Compiler Version 1.4.2 [Linux: amd64]
Compiled at 2020-11-30
Copyright (c) 2006-2020 by Andreas Rumpf

git hash: 3fb5157ab1b666a5a5c34efde0f357a82d433d04
active boot switches: -d:release

this is because globals are registered to the global variable table after init is run, meaning that any memory allocated while initializing globals will be collected by the gc

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions