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

Anton Tichawa anton.tichawa at chello.at
Wed Jun 23 20:40:24 CEST 2004


Matt Emson wrote:

>>>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
>
>
>_______________________________________________
>fpc-pascal maillist  -  fpc-pascal at lists.freepascal.org
>http://lists.freepascal.org/mailman/listinfo/fpc-pascal
>
>  
>
that should read:

..
begin
  a := TObject.Create;  // a points to a newly created object
  b := a;  // b now points to the same object (that is already pointed 
to by a)
..

anton.






More information about the fpc-pascal mailing list