How to free memory when an Out-of-memory exception occurs in Delphi using SetLength

I have a piece of Delphi code

var
  a: array of array of array of integer;
begin
  try
   SetLength(a, 100000, 100000, 10000); // out of memory here
   doStuffs(a); 
  except
   a = nil; // try to free the memory 
  end;
end;

The above code is trying to allocate a lot of memory, but out-of-memorywill be detected. Running a=nil, but memory is not freed.

Is there a way to free memory in case of an exception from memory?

I tried SetLength(a, 0, 0, 0)and Finalize(a), and both of them will not work either.

+5
source share
1 answer

In general, it is not possible to recover due to a memory error. At this point, the heap is most likely damaged. The appropriate response is to terminate the process.

DynArraySetLength System. . DynArraySetLength - , a , . DynArraySetLength, . , . , .

, DynArraySetLength , . . - , . , .

+7

All Articles