On 19 Jun 2008, at 15:20, Boian Mitov wrote:

never call Free() on interfaced object. I surely know that if I have used interfaces for the last 10 years or so ;-) . What I need is a way to make sure that all the references are gone. This is the first non garbage collected compiler that has prevented me from releasing all the references so far. Delphi, the C++ COM libraries VB etc. they all have behaved in a predictable way.

Delphi doesn't, look at this example:

***
uses
  SysUtils;

type
  imyintf = interface
    function getrefcount: longint;
  end;

  tmyclass = class(tinterfacedobject,imyintf)
    function getrefcount: longint;
  end;


  function tmyclass.getrefcount: longint;
    begin
      result:=refcount;
    end;

procedure test;
var
  i: imyintf;

  function test2: imyintf;
    begin
      result:=tmyclass.create;
      writeln(i.getrefcount);
    end;

begin
  i:=tmyclass.create;
  i:=test2;
  writeln(i.getrefcount);
end;

begin
  test;
end.
***

The output is (when compiled with Kylix 3):
refcount of i in test2: 1
refcount of i after calling test2: 2

If you comment out the "writeln" in test2, then the result is:
refcount of i after calling test2: 1

The reason is that if you access "i" inside test2, then Delphi cannot perform the optimisation of skipping the temp, and it also only frees the temp at the end of the function.

The behaviour you are counting on is a side effect of an optimisation, not a behaviour by design.


Jonas
_______________________________________________
fpc-devel maillist  -  fpc-devel@lists.freepascal.org
http://lists.freepascal.org/mailman/listinfo/fpc-devel

Reply via email to