[fpc-pascal] Reference Counting

Graeme Geldenhuys graemeg.lists at gmail.com
Sat Apr 3 22:59:00 CEST 2010


On 3 April 2010 21:01, Werner Van Belle <werner at yellowcouch.org> wrote:
>
> Type
>   TReferenceCount = Class


That is the same as...

   TReferenceCount = class(TObject)


> var
>   Test    : TReferenceCount;
>   Pest    : TReferenceCount;
> begin
>   Test:=TReferenceCount.Create();

...snip...

>   Test:=nil;

This causes a memory leak - you did not actually free the class
instance. You need to call:  Test.Free;


>   Pest:=nil;

Pest was just a reference pointer to the first instance, so no need to
free it. But setting it to nil after Test was freed is a good habit.

If you wanted real reference counted objects that gets freed
automatically when nothing is referencing it, then create a descendant
of TInterfacedObject.


-- 
Regards,
  - Graeme -


_______________________________________________
fpGUI - a cross-platform Free Pascal GUI toolkit
http://opensoft.homeip.net/fpgui/



More information about the fpc-pascal mailing list