[fpc-pascal] Memory allocation problem

Yann Bat batyann811 at gmail.com
Fri Feb 26 10:29:05 CET 2010


Hi,

I'm looking for a function or a procedure that tries to allocate
memory and return nil on failure.

GetMem seems to be a good candidate but it doesn't works as expected.
The FreePascal documentations
(http://www.freepascal.org/docs-html/rtl/system/getmem.html) tells
that it returns nil on failure but it raises an out of memory
exception (may be bug ???) when I try to allocates too much memory on
linux (I haven't try on windows).

Since my code is in fpc mode I can't catch the exeption. I know that
there is a ReturnNilIfGrowHeapFails variable but this changes the
behaviour of the all memory allocation system... I think I can do
something like the following code but I fear it's not thread safe.

function GetMemOrNil(size: PtrInt): pointer;
var
  tmp : boolean;
begin
  tmp:=ReturnNilIfGrowHeapFails;
  ReturnNilIfGrowHeapFails:=true;
  GetMemOrNil:=GetMem(size);
  ReturnNilIfGrowHeapFails:=tmp;
end;

Yann

Ps : Sorry for poor english.  I hope you can understand me.



More information about the fpc-pascal mailing list