Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2010-02

Memory allocation problem

2 messages · 1 person ·

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Yann Bat

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.

Yann Bat
to Yann Bat

Please forgot my stupid question.

GetMem works perfectly as described in the documentation. My problem cames from an another part of my program.

Yann