summary refs log tree commit diff stats
diff options
context:
space:
mode:
authoralaviss <leorize+oss@disroot.org>2020-08-17 18:20:48 +0000
committerGitHub <noreply@github.com>2020-08-17 20:20:48 +0200
commite9df8ebcfd475a715756d24e1718a5c1455689d2 (patch)
tree06380aad49cddd8151f5a28dad8bbf6ca03ead3a
parent6feb3900b121a356a1c091855008d080223ea802 (diff)
downloadNim-e9df8ebcfd475a715756d24e1718a5c1455689d2.tar.gz
gc_regions: cleanup & fixes for deallocation (#11920)
* gc_regions: withRegion nows return the modified MemRegion

* gc_regions: make withScratchRegion dealloc correctly

* tests/gc: add tregionleak test

This test checks if memory within regions are freed properly.
-rw-r--r--lib/system/gc_regions.nim9
-rw-r--r--tests/gc/tregionleak.nim23
2 files changed, 27 insertions, 5 deletions
diff --git a/lib/system/gc_regions.nim b/lib/system/gc_regions.nim
index 4f802c812..9425e040a 100644
--- a/lib/system/gc_regions.nim
+++ b/lib/system/gc_regions.nim
@@ -87,13 +87,13 @@ var
   tlRegion {.threadVar.}: MemRegion
 #  tempStrRegion {.threadVar.}: MemRegion  # not yet used
 
-template withRegion*(r: MemRegion; body: untyped) =
+template withRegion*(r: var MemRegion; body: untyped) =
   let oldRegion = tlRegion
   tlRegion = r
   try:
     body
   finally:
-    #r = tlRegion
+    r = tlRegion
     tlRegion = oldRegion
 
 template inc(p: pointer, s: int) =
@@ -262,14 +262,13 @@ when false:
     setObstackPtr(obs)
 
 template withScratchRegion*(body: untyped) =
-  var scratch: MemRegion
   let oldRegion = tlRegion
-  tlRegion = scratch
+  tlRegion = MemRegion()
   try:
     body
   finally:
+    deallocAll()
     tlRegion = oldRegion
-    deallocAll(scratch)
 
 when false:
   proc joinRegion*(dest: var MemRegion; src: MemRegion) =
diff --git a/tests/gc/tregionleak.nim b/tests/gc/tregionleak.nim
new file mode 100644
index 000000000..277cfc987
--- /dev/null
+++ b/tests/gc/tregionleak.nim
@@ -0,0 +1,23 @@
+discard """
+  cmd: '''nim c --gc:regions $file'''
+  output: '''
+finalized
+finalized
+'''
+"""
+
+proc finish(o: RootRef) =
+  echo "finalized"
+
+withScratchRegion:
+  var test: RootRef
+  new(test, finish)
+
+var
+  mr: MemRegion
+  test: RootRef
+
+withRegion(mr):
+  new(test, finish)
+
+deallocAll(mr)