@@ -240,6 +240,7 @@ public final class Process: ObjectIdentifierProtocol {
240240 var attributes = posix_spawnattr_t ( )
241241 #endif
242242 posix_spawnattr_init ( & attributes)
243+ defer { posix_spawnattr_destroy ( & attributes) }
243244
244245 // Unmask all signals.
245246 var noSignals = sigset_t ( )
@@ -283,6 +284,7 @@ public final class Process: ObjectIdentifierProtocol {
283284 var fileActions = posix_spawn_file_actions_t ( )
284285 #endif
285286 posix_spawn_file_actions_init ( & fileActions)
287+ defer { posix_spawn_file_actions_destroy ( & fileActions) }
286288
287289 // Workaround for https://sourceware.org/git/gitweb.cgi?p=glibc.git;h=89e435f3559c53084498e9baad22172b64429362
288290 let devNull = strdup ( " /dev/null " )
@@ -319,16 +321,15 @@ public final class Process: ObjectIdentifierProtocol {
319321 throw SystemError . posix_spawn ( rv, arguments)
320322 }
321323
322- posix_spawn_file_actions_destroy ( & fileActions)
323- posix_spawnattr_destroy ( & attributes)
324-
325324 if redirectOutput {
326325 // Close the write end of the output pipe.
327326 try close ( fd: & outputPipe[ 1 ] )
328327
329328 // Create a thread and start reading the output on it.
330- var thread = Thread {
331- self . stdout. result = self . readOutput ( onFD: outputPipe [ 0 ] )
329+ var thread = Thread { [ weak self] in
330+ if let readResult = self ? . readOutput ( onFD: outputPipe [ 0 ] ) {
331+ self ? . stdout. result = readResult
332+ }
332333 }
333334 thread. start ( )
334335 self . stdout. thread = thread
@@ -337,8 +338,10 @@ public final class Process: ObjectIdentifierProtocol {
337338 try close ( fd: & stderrPipe[ 1 ] )
338339
339340 // Create a thread and start reading the stderr output on it.
340- thread = Thread {
341- self . stderr. result = self . readOutput ( onFD: stderrPipe [ 0 ] )
341+ thread = Thread { [ weak self] in
342+ if let readResult = self ? . readOutput ( onFD: stderrPipe [ 0 ] ) {
343+ self ? . stderr. result = readResult
344+ }
342345 }
343346 thread. start ( )
344347 self . stderr. thread = thread
0 commit comments