GetHeapStatus.TotalAllocated is wrong/misleading in multithreaded application
Original Reporter info from Mantis: n.zhubr
-
Reporter name: Nikolai Zhubr
Original Reporter info from Mantis: n.zhubr
- Reporter name: Nikolai Zhubr
Description:
In a multithreaded application, if one thread allocates mem block from the heap and later another thread deallocates it, GetHeapStatus.TotalAllocated after deallocation reports still the same value as before deallocation, in both threads.
Steps to reproduce:
Run the included example program. The output will be:
In program: 0 M
In thread: 2 M
In thread: 2 M
Deallocated! <<<=== After this, TotalAllocated should go down somewhere?
In program: 0 M
In thread: 2 M <<<=== ... but it doesn't!
In program: 0 M
In thread: 2 M
Additional information:
program heapstatustest;
{$mode delphi}
{$apptype console}
uses
SysUtils, Classes;
type
TMyThread = class(TThread)
procedure Execute; override;
end;
procedure ShowAllocatedSize(s: string);
begin
writeln(s + IntToStr(GetHeapStatus.TotalAllocated shr 20) + ' M');
end;
var
p: pointer = nil;
procedure TMyThread.Execute;
begin
GetMem(p, 1024*1024*2);
repeat
ShowAllocatedSize('In thread: ');
sleep(1000);
until false;
end;
begin
TMyThread.create(false);
repeat
if p <> nil then
begin
FreeMem(p, 1024*1024*2);
p := nil;
writeln('Deallocated!');
end;
ShowAllocatedSize('In program: ');
sleep(1000);
until false;
end.
Mantis conversion info:
- Mantis ID: 15763
- OS: windows (and probably others)
- OS Build: XP SP3
- Platform: i386 (and probably others)
- Version: 2.4.0