This isn't what @boia01 meant at all, you need to make a safe reference, not cast the raw pointer to a traced Nim reference, that's invalid code and it'll likely crash.
An example of working code would be this: # Example of proc memory capture violation import flatty, # https://github.com/treeform/flatty - for actual persistence std/[exitprocs, os, times] type HistoryEntry = string History = ref object entries: seq[HistoryEntry] proc save(history: History, filename: string) = writeFile(filename, history.toFlatty) proc persistence(history: var History, filename: string) = if fileExists filename: history = filename.readFile.fromFlatty History let history = history # make a non-var history so we can capture it addExitProc( proc() = save(history, filename) # Error: 'history' is of type <var History> which cannot be captured as # it would violate memory safety, declared here: # /Users/DM/work/trunk/nim/test_persistence.nim(10, 11); using # '-d:nimNoLentIterators' helps in some cases ) # Example usage const historyFilename = "history.his" var history = History() history.persistence historyFilename echo history.entries # so far history.entries.add "Another entry: " & $now() # Add another entry on each run Run There are probably other ways, but this is what I came up with.