summary refs log tree commit diff stats
path: root/lib/pure/osproc.nim
diff options
context:
space:
mode:
authorAraq <rumpf_a@web.de>2011-11-18 22:14:32 +0100
committerAraq <rumpf_a@web.de>2011-11-18 22:14:32 +0100
commita497b4d1cf529b24ac469e701cb60e8d8b5fdefc (patch)
tree867eb30d869f96ab3bec0908f62e1f26bc187a82 /lib/pure/osproc.nim
parentc9b67f724d039437ea72f666018d2a80266b8a2b (diff)
downloadNim-a497b4d1cf529b24ac469e701cb60e8d8b5fdefc.tar.gz
bugfix: fixed memory leaks in osproc module
Diffstat (limited to 'lib/pure/osproc.nim')
-rwxr-xr-xlib/pure/osproc.nim34
1 files changed, 18 insertions, 16 deletions
diff --git a/lib/pure/osproc.nim b/lib/pure/osproc.nim
index b1b22f0b8..03c3def4d 100755
--- a/lib/pure/osproc.nim
+++ b/lib/pure/osproc.nim
@@ -224,14 +224,6 @@ when not defined(useNimRtl):
     close(outp)
     close(p)
 
-when false:
-  proc deallocCStringArray(a: cstringArray) =
-    var i = 0
-    while a[i] != nil:
-      dealloc(a[i])
-      inc(i)
-    dealloc(a)
-
 when defined(Windows) and not defined(useNimRtl):
   # We need to implement a handle stream for Windows:
   type
@@ -491,6 +483,13 @@ elif not defined(useNimRtl):
       copyMem(result[i], addr(x[0]), x.len+1)
       inc(i)
 
+  proc deallocCStringArray(a: cstringArray) =
+    var i = 0
+    while a[i] != nil:
+      dealloc(a[i])
+      inc(i)
+    dealloc(a)
+    
   proc startProcess(command: string,
                  workingDir: string = "",
                  args: openarray[string] = [],
@@ -537,18 +536,21 @@ elif not defined(useNimRtl):
           chck posix_spawn_file_actions_adddup2(fops, p_stderr[writeIdx], 2)
       
       var e = if env == nil: EnvToCStringArray() else: ToCStringArray(env)
-      
+      var a: cstringArray
+      var res: cint
       if workingDir.len > 0: os.setCurrentDir(workingDir)
       if poUseShell notin options:
-        var a = toCStringArray([extractFilename(command)], args)
-        chck posix_spawn(pid, command, fops, attr, a, e)
+        a = toCStringArray([extractFilename(command)], args)
+        res = posix_spawn(pid, command, fops, attr, a, e)
       else:
         var x = addCmdArgs(command, args)
-        var a = toCStringArray(["sh", "-c"], [x])
-        chck posix_spawn(pid, "/bin/sh", fops, attr, a, e)
-
-      chck posix_spawn_file_actions_destroy(fops)
-      chck posix_spawnattr_destroy(attr)
+        a = toCStringArray(["sh", "-c"], [x])
+        res = posix_spawn(pid, "/bin/sh", fops, attr, a, e)
+      deallocCStringArray(a)
+      deallocCStringArray(e)
+      discard posix_spawn_file_actions_destroy(fops)
+      discard posix_spawnattr_destroy(attr)
+      chck res
 
     else: