I have multithreaded application as I ask here.
I want to terminate the thread, and start a new one when following method is called.
procedure TFRABData.RefreshDataset;
var
GridUpdater: TGridUpdater;
begin
if Assigned(updaterThread) and (updaterThread <> nil) then
begin
updaterThread.Terminate;
end;
GridUpdater := TGridUpdater.Create(True);
GridUpdater.OwnerForm := Self;
updaterThread := GridUpdater;
GridUpdater.FreeOnTerminate := False;
GridUpdater.Start;
CodeSite.Send('RefreshDataset executed');
end
but, when FreeOnTerminate set to True, I get Access Violation, but when FreeOnTerminate set to False, I get memory leak. How to free the thread?
And in addition to RRUZ’s answer, to let it work with
FreeOnTerminate = False:Terminatejust sets the flag, it does nothing more.Change
to
Freewill callTerminateandWaitForsubsequently to eliminate your memory leak.