Reputation: 629
for x:=0 to NumberOfWaitingThreads-1 do
begin
WaitForThread:=TWaitForThread.Create(true);
ArrayOfHandles[x]:=WaitForThread.Handle;
WaitForThread.FreeOnTerminate:=false;
WaitForThread.CommandLineList:=TStringList.Create;
WaitForThread.CommandLineList.Text:=CommandList.Text;
WaitForThread.Resume;
end;
CommandList.Free;
repeat
WaitStatus:=WaitForMultipleObjects(NumberOfWaitingThreads,@ArrayOfHandles[0], True, 100);
until WaitStatus<>WAIT_TIMEOUT;
FreeAndNil(WaitForThread);
Does FreeAndNil(WaitForThread) also free TStringList created here WaitForThread.CommandLineList:=TStringList.Create;
Upvotes: 0
Views: 302
Reputation: 34929
Does
FreeAndNil(WaitForThread)
also freeTStringList
created hereWaitForThread.CommandLineList:=TStringList.Create;
FreeAndNil(WaitForThread)
will only free the thread itself, not any objects created by the thread.
So, the answer is no, you will have to do that in the thread destructor.
Note that if NumberOfWaitingThreads
> 1, you will leak all thread objects but the last one. Fix that by declaring an array of TWaitForThread
:
var
WaitForThreadArr : array of TWaitForThread;
...
SetLength(WaitForThreadArr,NumberOfWaitingThreads);
for x := 0 to NumberOfWaitingThreads-1 do
begin
WaitForThreadArr[x] := TWaitForThread.Create(true);
ArrayOfHandles[x] := WaitForThreadArr[x].Handle;
WaitForThreadArr[x].FreeOnTerminate := false;
WaitForThreadArr[x].CommandLineList := TStringList.Create;
WaitForThreadArr[x].CommandLineList.Text := CommandList.Text;
WaitForThreadArr[x].Start;
end;
CommandList.Free;
repeat
WaitStatus := WaitForMultipleObjects(NumberOfWaitingThreads,@ArrayOfHandles[0], True, 100);
until WaitStatus <> WAIT_TIMEOUT;
for x := 0 to NumberOfWaitingThreads - 1 do begin
Free(WaitForThreadArr[x]);
end;
Upvotes: 3