[fpc-pascal]Memory leak in freepascal, FreeAndNil not working properly?

Matt Emson memsom at interalpha.co.uk
Wed Jun 23 17:58:04 CEST 2004


> > You code is wrong, it also crashes with kylix.
>
> What kind of answer is this?

The correct one?

To remove an item from a TList you must:

(a) set it to nil and pack the list

e.g.

  list[0] := nil;
  list.pack;

(b) call TList.Delete(index)

e.g.

  list.delete(0);

(c) use TList.Remove(pointer)

e.g.

  list.remove(t);

Did the explanation not make sense? You obviusly do not understand how
pointers wor... right? A pointer points to a memory address. Setting a
pointer to nil, does not remove that block of memory from existance... for
example:

var
  a, b: TObject;
begin
  a := TObject.Create;
  b := a; //b points at a
  freeandnil(a);
  //at this point, b still points to the memory allocated to a, but this
memory is no longer valid, and it will A/V if you free b

  b.free; //access violation...
end;

Do you understand now?

Matt





More information about the fpc-pascal mailing list