[fpc-pascal] ReAllocMem problem (?)

Marcel Martin mm10 at ellipsa.net
Tue Jan 18 04:31:12 CET 2005


Hello,

The following function SysReAllocMem comes from the file
/rtl/inc/heap.inc (FPC 1.9.7 / Win32)

function SysReAllocMem(var p: pointer; size: ptrint):pointer;
[...]
   { Resize block }
   if not SysTryResizeMem(p,size) then
   begin
     minsize := MemoryManager.MemSize(p);
     if size < minsize then
       minsize := size;
     p2 := MemoryManager.AllocMem(size);
     if p2<>nil then
       Move(p^,p2^,minsize);
     MemoryManager.FreeMem(p);
     p := p2;
   end;
[...]

Maybe you have good reasons to write it like you did but is 
the instruction "MemoryManager.FreeMem(p);" intentionally
always executed? 
I thought that, in case it cannot increase the size of the 
memory allocated to p^, the memory manager would set p to nil 
but not that it would free it.

Suppose OneClass has a field FP which points to an array of
pointers. In case of problem when attempting to increase the
size of FP^, FP is freed and OneClass can no more destroy the 
pointed objects it created.

Up to now, I believed the code was something like

  if p2<>nil then
  begin
    Move(p^,p2^,minsize);
    MemoryManager.FreeMem(p);
  end;
  p := p2; <- p might be set to nil but it is not freed

and, in OneClass, one could do

  Q := FP;
  ReAllocMem(FP, NewSize (greater than current size));
  if FP = nil then
  begin
    FP := Q;
    Signal the 'Out of Memory' problem
  end; 

Was your programming intentional? And if so, why?
Thanks.

-- 
mm
http://www.ellipsa.net/




More information about the fpc-pascal mailing list