Missing memory in TBitmap

Writing a program showing large bitmaps I
found that bitmaps larger than 64k allocate
twice as much memory than needed, which in
my case is about 500k.
The problem is that in the graphics unit
memory is allocated using MemAlloc function:

function MemAlloc(Size: Longint): Pointer;
  Handle: THandle;
  if Size < 65535 then
    GetMem(Result, Size)
    Handle := GlobalAlloc(HeapAllocFlags, Size);
    Result := GlobalLock(Handle);

For size larger than 64k MemAlloc uses WinAPI
functions GlobalAlloc and GlobalLock. So far
everything is Ok, but memory allocated with
this function is deallocated with a Delphi
function FreeMem! (in ReadWin3DIB procedure,
to say one)

BitsMem := MemAlloc(ImageSize);
FreeMem(BitsMem, ImageSize);

Definitely, FreeMem cannot deallocate memory
allcated with GlobalAlloc and does nothing
(though it would be nice if it could raise
an exception).

Fixing this problem takes just several lines,
for example one can add function FreeMem in
the implementation section of the graphics
unit, which looks like its counterpart MemAlloc
and forces all the methods in this unit to
use your FreeMem rather than one from
the system unit:

procedure FreeMem(var P; Size: Longint);
 if Size < 65535 then
    System.FreeMem(Pointer(P), Size)

Note that Size is now of the type longint,
not word.


Dmitry Streblechenko (dmit...@phyast.la.asu.edu)