Przeglądaj źródła

fcl-base: fpthreadpool: Dispose TThreadPoolList threads to prevent leaking memory

Oleg Latov 2 tygodni temu
rodzic
commit
e9dc21e4bc
1 zmienionych plików z 23 dodań i 1 usunięć
  1. 23 1
      packages/fcl-base/src/fpthreadpool.pp

+ 23 - 1
packages/fcl-base/src/fpthreadpool.pp

@@ -472,7 +472,29 @@ begin
 end;
 
 destructor TFPCustomSimpleThreadPool.TThreadPoolList.Destroy;
-begin
+var
+  Threads: array of TThread = ();
+  Thread: TThread;
+  TempList: TList;
+  I: Integer;
+begin
+  TempList := FList.LockList;
+
+  { Copy threads to a separate array to avoid holding the lock while terminating threads }
+  SetLength(Threads, TempList.Count);
+  for I := 0 to TempList.Count - 1 do
+    Threads[I] := TThread(TempList[I]);
+
+  FList.UnlockList;
+
+  { Now terminate and free threads outside the lock }
+  for Thread in Threads do
+  begin
+    Thread.Terminate;
+    Thread.WaitFor;
+    Thread.Free;
+  end;
+
   FreeAndNil(FList);
   Inherited;
 end;