I am using TStopWatch
for high-precision timekeeping in Delphi 10.2 Tokyo.
This website: https://www.thoughtco.com/accurately-measure-elapsed-time-1058453 has given the following example:
var
sw : TStopWatch;
elapsedMilliseconds : cardinal;
begin
sw := TStopWatch.Create() ;
try
sw.Start;
//TimeOutThisFunction()
sw.Stop;
elapsedMilliseconds := sw.ElapsedMilliseconds;
finally
sw.Free;
end;
end;
Apparently, there is a mistake there, since:
StopWatch
does not containFree
- Delphi documentation clearly states that:
TStopwatch
is not a class but still requires explicit initialization [usingStartNew
orCreate
methods].
This is confusing. I am using TStopWatch
in a function, and I am not using free
. This function may be called multiple times during each session (perhaps hundreds of times, depending on the usage). It means multiple instances of TStopWatch
will be created, without being freed.
Is there a possibility of memory leaks, or other complications? If the answer is yes, what am I supposed to do? Do I have to create only one instance of TStopWatch
per application? Or should I use other functions? Or something else?