Просмотр исходного кода

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

Oleg Latov 2 недель назад
Родитель
Сommit
e9dc21e4bc
1 измененных файлов с 23 добавлено и 1 удалено
  1. 23 1
      packages/fcl-base/src/fpthreadpool.pp

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

@@ -472,7 +472,29 @@ begin
 end;
 end;
 
 
 destructor TFPCustomSimpleThreadPool.TThreadPoolList.Destroy;
 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);
   FreeAndNil(FList);
   Inherited;
   Inherited;
 end;
 end;