Board index » delphi » MemAlloc size is LONGINT but FreeMem size is ONLY WORD

MemAlloc size is LONGINT but FreeMem size is ONLY WORD

I allocated about 1 million bytes with Memalloc, but I get a range check
error when trying to freeup that very same memory with FreeMem.  This is
because the FreeMem size is only a word.  What is the work around for this
problem.  I'm using Delphi 1.  It appears that D2 might work, since the
cardinal size is 32 bits, but my app must run in a 16 bit environment.

 

Re:MemAlloc size is LONGINT but FreeMem size is ONLY WORD


On 22 Dec 1996 15:03:41 GMT, b...@aa.net (Bob Richardson) wrote:

Quote
>I allocated about 1 million bytes with Memalloc, but I get a range check
>error when trying to freeup that very same memory with FreeMem.  This is
>because the FreeMem size is only a word.  What is the work around for this
>problem.  I'm using Delphi 1.  It appears that D2 might work, since the
>cardinal size is 32 bits, but my app must run in a 16 bit environment.

This problem comes up frequently. Write a new function that calls
GlobalFreePtr if the size is > $FFFF and calls FreeMem otherwise.
(Borland's solution is to turn off range checking, but that results in
a memory leak when the size is an integral multiple of 64K.)

--
Ray Lischner, Tempest Software, Inc., Corvallis, Oregon, USA
Author of Secrets of Delphi 2 (http://www.tempest-sw.com/secrets/)

Re:MemAlloc size is LONGINT but FreeMem size is ONLY WORD


Quote
Bob Richardson wrote:

> I allocated about 1 million bytes with Memalloc, but I get a range check
> error when trying to freeup that very same memory with FreeMem.  This is
> because the FreeMem size is only a word.  What is the work around for this
> problem.  I'm using Delphi 1.  It appears that D2 might work, since the
> cardinal size is 32 bits, but my app must run in a 16 bit environment.

Hellow Rob!
Without long discussion i only write what functions can you use.
There are not all but i think it will be enough ;)
for short memory allocatin {1..65520 bytes}:
                        {this function use pointer}
  GetMem                {Description is in help file}
  FreeMem;              {Description is in help file}
for big memory allocations {1..>65536} API :
                        {this function use memory handle}
  GlobalAllock          {Description is in help file}
  GlobalFree            {Description is in help file}
  GlobalLock            {Description is in help file}
  GlobalReallock        {Description is in help file}
for big memory allocations {1..>65536}:
                        {this function decribed only in Winprocs unit and using Pointer}
  function GlobalPtrHandle(P: Pointer): THandle;
  function GlobalAllocPtr(Flags: Word; Bytes: Longint): Pointer;
  function GlobalReAllocPtr(P: Pointer; Bytes: Longint; Flags: Word): Pointer;
  function GlobalFreePtr(P: Pointer): THandle;
  function GlobalLockPtr(P: Pointer): Pointer;
  function GlobalUnlockPtr(P: Pointer): Bool;

At the end there is function MemAlloc here (Graphics unit)
as it is written by Borland.

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

-----------------------------  
Bye!

Re:MemAlloc size is LONGINT but FreeMem size is ONLY WORD


Quote
b...@aa.net (Bob Richardson) wrote:
>I allocated about 1 million bytes with Memalloc, but I get a range check
>error when trying to freeup that very same memory with FreeMem.  This is
>because the FreeMem size is only a word.  What is the work around for this
>problem.  I'm using Delphi 1.  It appears that D2 might work, since the
>cardinal size is 32 bits, but my app must run in a 16 bit environment.

why dont you try to write 32-Bit applications and use them under
win32s? There is not too much difference. But there are also
memory-bugs, specially with win32s and delphi2, but I wrote an
alternate memory-manager, which works really good.

hope, that it helps, Markus

xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
Be fast, the next pascal-upgrades are under work!!

Markus Pfau

Markus.P...@post.rwth-aachen.de

Other Threads