GC safety effect

We call a proc p GC safe when it doesn’t access any global variable that contains GC’ed memory (string, seq, ref or a closure) either directly or indirectly through a call to a GC unsafe proc.

The GC safety property is usually inferred. The inference for GC safety is analogous to the inference for exception tracking.

The gcsafe annotation can be used to mark a proc to be gcsafe, otherwise this property is inferred by the compiler. Note that noSideEffect implies gcsafe.

Routines that are imported from C are always assumed to be gcsafe.

To override the compiler’s gcsafety analysis a {.cast(gcsafe).} pragma block can be used:

  1. var
  2. someGlobal: string = "some string here"
  3. perThread {.threadvar.}: string
  4. proc setPerThread() =
  5. {.cast(gcsafe).}:
  6. deepCopy(perThread, someGlobal)

See also: