ReallocMem may result in wrong GetHeapStatus results
Original Reporter info from Mantis: n.zhubr
-
Reporter name: Nikolai Zhubr
Original Reporter info from Mantis: n.zhubr
- Reporter name: Nikolai Zhubr
Description:
Some reallocation sequences trigger MemRealloc misbehaviour.
This can be most easily observed by noticing that GetHeapStatus.TotalAllocated gets negative. However, I also made some tests which hooked GetMem/FreeMem instead. From these tests it looks like sometimes FreeMem is called more than once for the same pointer (that is, without obtaining that pointer again in between).
Steps to reproduce:
a) on windows, put the included example#1 in a file named 000.pas (with other filename misbehaviour might not trigger!), compile with 2.4.0 (do not add any options to compiler) and run. Then (hopefully) you will see negative TotalAllocated values:
4194281K &LtPos;enter>
4194257K &LtPos;enter>
4194233K <...>
OR, if it does not reproduce or is not sufficient,
b) try the included example#2 (hopefully it is more reliable and platform-independent, but it makes much more iterations and employs random numbers)
Additional information:
/////////////////////////// example#1 /////////////////////////////
{$mode delphi}
{$apptype console}
uses
SysUtils;
const
data: array [0.. 12] of integer = (9000, 1000, 9000, 12, 9000, 26, 9000, 74, 9000, 1, 2000, 9000, 1);
var
p: pointer;
i: integer;
begin
p := nil;
repeat
for i := low(data) to high(data) do
ReAllocMem(p, data[i]*4);
writeln((GetHeapStatus.TotalAllocated shr 10), 'K');
readln;
until false;
end.
/////////////////////////// example#2 /////////////////////////////
{$apptype console}
var
p: pointer;
trig: boolean;
tmp, count: longint;
begin
writeln('This may take a few seconds...');
p := nil;
count := 0;
repeat
inc(count);
tmp := random(10000);
ReAllocMem(p, tmp*4);
trig := longint(GetHeapStatus.TotalAllocated) < 0;
if trig then
writeln((GetHeapStatus.TotalAllocated shr 10), 'K (count=', count, ')');
until trig or (count > 100000);
if not trig then
writeln('The problem does not seem to happen this time.');
end.
///////////////////////////////////////////////////////////////////
Mantis conversion info:
- Mantis ID: 15805
- OS: windows (probably others too)
- OS Build: any
- Platform: i386
- Version: 2.4.0
- Monitored by: » Lord Horazont (Jonas Schäfer)