Skip to content

[ARC] {.global.} pragma doesn't work when the variable is initialized with non-value types #17552

Open
@ghost

Description

The title says it all, with ARC {.global.} only seems to work with value types (ints, bools, objects) or when declaration and Initialization of the global variable are separate.
Non-value types (ref objects, strings, and other types that are managed by the runtime) are always initialized each time if they have are declared with an assignment. Found out by investigating memory leak in https://forum.nim-lang.org/t/7709.

Example

proc main = 
  var tc {.global.} = "hi"
  tc &= "hi"
  echo tc

main()
main()
main()

Current Output

ARC/ORC:

hihi
hihi
hihi

refc:

hihi
hihihi
hihihihi

On the contrary, this works just fine:

proc main = 
  var tc {.global.}: string
  tc &= "hi"
  echo tc

main()
main()
main()

I think that the compiler should show a warning when {.global.} pragma is used with ARC/ORC, or at least when it's used with non-value types. Or we can deprecate it altogether

Additional Information

This might be related to #15005.

$ nim -v
Nim Compiler Version 1.5.1 [Linux: amd64]
Compiled at 2021-03-28
Copyright (c) 2006-2021 by Andreas Rumpf

git hash: eb3ed44009fdfea3e67608b68a049c265124d643
active boot switches: -d:release

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