From 3331c5e7af28bd2ee56a1080a1829e3fb6747f60 Mon Sep 17 00:00:00 2001 From: Littleote <57794658+Littleote@users.noreply.github.com> Date: Mon, 22 Jan 2024 14:20:25 +0100 Subject: [PATCH] Free threads in std.Thread.Pool.init only with pool.join Free the allocated threads in the initialization of a thread pool only with pool.join instead of additionally calling allocator.free causing free to be called twice. Resolves #18643 --- lib/std/Thread/Pool.zig | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/lib/std/Thread/Pool.zig b/lib/std/Thread/Pool.zig index ed1a4dc052..3694f94be4 100644 --- a/lib/std/Thread/Pool.zig +++ b/lib/std/Thread/Pool.zig @@ -35,10 +35,9 @@ pub fn init(pool: *Pool, options: Options) !void { } const thread_count = options.n_jobs orelse @max(1, std.Thread.getCpuCount() catch 1); - pool.threads = try allocator.alloc(std.Thread, thread_count); - errdefer allocator.free(pool.threads); - // kill and join any threads we spawned previously on error. + // kill and join any threads we spawned and free memory on error. + pool.threads = try allocator.alloc(std.Thread, thread_count); var spawned: usize = 0; errdefer pool.join(spawned);