среда, 15 апреля 2015 г.

Today it seemed to me I found an error in Delphi XE memory manager

Original in Russian: http://18delphi.blogspot.ru/2013/04/delphi-xe.html

All symptoms indicated it.

I even specified a test:
procedure TReallocMemTest.DoIt;
var
 l_Index : Integer;
 l_P : PAnsiChar;
 l_I : Integer;
 l_Size : Integer;
 l_OldSize : Integer;
begin
 for l_Index := 0 to 5000 do
 begin
  l_Size := 10;
  GetMem(l_P, l_Size);
  try
   while (l_Size <= 6144 * 2) do
   begin
    FillChar(l_P^, l_Size, Random($ff));
    l_OldSize := l_Size;
    l_I := PInteger(l_P + l_OldSize - SizeOf(Integer))^;
    Inc(l_Size, Random(20));
    ReallocMem(l_P, l_Size);
    Check(PInteger(l_P + l_OldSize - SizeOf(Integer))^ = l_I);
   end;//l_Size <= 6144
  finally
   FreeMem(l_P);
  end;//try..finally
 end;//for l_Index
end;//TReallocMemTest.DoIt
 
procedure TReallocMemTest.DoIt1;
const
 cMagicSize0 = 10;
 cMagicSize = 3120;
 cNewMagicSize = 6144;
var
 l_P : PAnsiChar;
 l_I : Integer;
begin
 GetMem(l_P, cMagicSize0);
 try
  FillChar(l_P^, cMagicSize0, 10);
  l_I := PInteger(l_P + cMagicSize0 - SizeOf(Integer))^;
  ReallocMem(l_P, cNewMagicSize);
  Check(PInteger(l_P + cMagicSize0 - SizeOf(Integer))^ = l_I);
 
  FillChar(l_P^, cMagicSize, 10);
  l_I := PInteger(l_P + cMagicSize - SizeOf(Integer))^;
  ReallocMem(l_P, cNewMagicSize);
  Check(PInteger(l_P + cMagicSize - SizeOf(Integer))^ = l_I);
 finally
  FreeMem(l_P);
 end;//try..finall
end;//TReallocMemTest.DoIt

I even told my colleagues about my “discovery”. I even wrote to Embarcadero.

But! This test does not fall if you do not call MY CODE first.

The ball is on my side.

I looked so stupid.

But only he that never climbed never fell. Instead, I added one more test to the base.

I’ll repeat that the written and DEBUGGED test should not be thrown but added to the test base. It took our “nerves and blood”.

It reminds me of a Russian fairy-tale: “Don’t dead me, Ivanushka the Fool, you can make use of me”.

I hope, that I’ve become a bit wiser today.

P.S. The actual problems were in this “staff” - http://18delphi.blogspot.com/2015/02/how-to-find-out-true-size-of-memory.html

Комментариев нет:

Отправить комментарий