Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2010-04

Reference Counting

7 messages · 5 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Werner Van Belle

Hello Dear Freepascal users,

I'm a newbee to object pascal. I did however program in turbo pascal. The past couple of days I learned that there exist a variety of classes/object systems in freepascal. Now, I already figured out that the generic class generators are that what we are looking for, which means that we will be generating classes.

Now, as far as I understand it, these classes are in a sense 'smart pointers' (yes, I come from a C++ env), and I found some hints at various places that automatic reference counting should be available in object pascal. So I tried a little testprogram to see when the objects instatiated from a class are disposed off. Below is the little program:

{$mode objfpc}
Type
   TReferenceCount = Class
 Public
   nummerke: Integer;
   Destructor Destroy;
end;
Destructor TReferenceCount.Destroy;
begin
   WriteLn('Deallocation');
end;
var
   Test    : TReferenceCount;
   Pest    : TReferenceCount;
begin
   Test:=TReferenceCount.Create();
   Pest:=Test;
   Test.nummerke:=100;
   WriteLn('Nummerke in test is ', Test.nummerke);
   WriteLn('Nummerke in pest is ', Pest.nummerke);
   Pest.nummerke:=200;
   WriteLn('Nummerke in test is ', Test.nummerke);
   WriteLn('Nummerke in pest is ', Pest.nummerke);
   Test:=nil;
   Pest:=nil;
end.

So, the question now is where I can find documentation on the reference counting mechanism in freepascal ?

Any help would be appreciated,

Wkr,

Reply by email

Graeme Geldenhuys
to Werner Van Belle
Werner Van Belle wrote
Type   TReferenceCount = Class

That is the same as...

TReferenceCount = class(TObject)

Show quoted text 5 lines
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/

Reply by email

Zaher Dirkey
to Graeme Geldenhuys
Graeme Geldenhuys wrote
On 3 April 2010 21:01, Werner Van Belle wrote:

If you wanted real reference counted objects that gets freed

automatically when nothing is referencing it, then create a descendant of TInterfacedObject.

But he must use variable of interface to use TInterfacedObject not variable of TObject to take this advantage (if i am not wrong).

Zaher Dirkey

Reply by email

Paul Ishenin
to Zaher Dirkey
Zaher Dirkey wrote
But he must use variable of interface to use TInterfacedObject not variable of TObject to take this advantage (if i am not wrong).

You are not wrong.

Best regards,
Paul Ishenin.

Reply by email

Werner Van Belle
to Graeme Geldenhuys: That is the same as... TReferenceCount = class(TObject) ...snip... This causes a memory…
Graeme Geldenhuys wrote 6 lines
> Test:=nil; > This causes a memory leak - you did not actually free the class instance. You need to call: Test.Free;

Of course I didn't call free; I was hoping that the reference counter would solve that little problem. If I need to free stuff myself, I don't really need a reference counter. In any case, thanks for the answer. I will give it a try and see what it gives

Show quoted text 6 lines
> 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.

I'm not sure that is correct. Once you free Test, Pest still points to the same non existing object and you can use assigned to check it,

Reply by email

cobines
to Werner Van Belle

2010/4/5 Werner Van Belle :

Show quoted text 5 lines
> 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. > I'm not sure that is correct. Once you free Test, Pest still points to the same non existing object and you can use assigned to check it,

Pest would be a dangling pointer and should not be accessed.

I attached sample code with interfaces for you.

cobines

Reply by email

Graeme Geldenhuys
to cobines
cobines wrote 6 lines
>> > I'm not sure that is correct. Once you free Test, Pest still points to > the same non existing object and you can use assigned to check it, Pest would be a dangling pointer and should not be accessed.

Correct, and that was what I was trying to get across. Accessing it after Test was freed will cause Access Violations, so setting it to nil is the correct thing to do.

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

Reply by email