summary refs log tree commit diff stats
diff options
context:
space:
mode:
-rw-r--r--compiler/cgen.nim4
-rw-r--r--tests/destructor/tgcdestructors.nim17
2 files changed, 20 insertions, 1 deletions
diff --git a/compiler/cgen.nim b/compiler/cgen.nim
index 4abefe463..cb186de10 100644
--- a/compiler/cgen.nim
+++ b/compiler/cgen.nim
@@ -316,7 +316,9 @@ proc resetLoc(p: BProc, loc: var TLoc) =
   let containsGcRef = containsGarbageCollectedRef(loc.t)
   let typ = skipTypes(loc.t, abstractVarRange)
   if isImportedCppType(typ): return
-  if not isComplexValueType(typ):
+  if p.config.selectedGc == gcDestructors and typ.kind in {tyString, tySequence}:
+    linefmt(p, cpsStmts, "$1.len = 0; $1.p = NIM_NIL;$n", rdLoc(loc))
+  elif not isComplexValueType(typ):
     if containsGcRef:
       var nilLoc: TLoc
       initLoc(nilLoc, locTemp, loc.lode, OnStack)
diff --git a/tests/destructor/tgcdestructors.nim b/tests/destructor/tgcdestructors.nim
new file mode 100644
index 000000000..60d7fc14f
--- /dev/null
+++ b/tests/destructor/tgcdestructors.nim
@@ -0,0 +1,17 @@
+discard """
+  cmd: '''nim c --gc:destructors $file'''
+  output: '''1 1'''
+"""
+
+import allocators
+include system / ansi_c
+
+proc main =
+  var s: seq[string] = @[]
+  for i in 0..<80: s.add "foo"
+
+main()
+
+#echo s
+let (a, d) = allocCounters()
+cprintf("%ld %ld\n", a, d)