Mixing GC’ed memory with ptr

Special care has to be taken if an untraced object contains traced objects like traced references, strings or sequences: in order to free everything properly, the built-in procedure reset has to be called before freeing the untraced memory manually:

  1. type
  2. Data = tuple[x, y: int, s: string]
  3. # allocate memory for Data on the heap:
  4. var d = cast[ptr Data](alloc0(sizeof(Data)))
  5. # create a new string on the garbage collected heap:
  6. d.s = "abc"
  7. # tell the GC that the string is not needed anymore:
  8. reset(d.s)
  9. # free the memory:
  10. dealloc(d)

Without the reset call the memory allocated for the d.s string would never be freed. The example also demonstrates two important features for low level programming: the sizeof proc returns the size of a type or value in bytes. The cast operator can circumvent the type system: the compiler is forced to treat the result of the alloc0 call (which returns an untyped pointer) as if it would have the type ptr Data. Casting should only be done if it is unavoidable: it breaks type safety and bugs can lead to mysterious crashes.

Note: The example only works because the memory is initialized to zero (alloc0 instead of alloc does this): d.s is thus initialized to binary zero which the string assignment can handle. One needs to know low level details like this when mixing garbage collected data with unmanaged memory.