Re: Reference counting bug D6

From: Joanna Carter \(TeamB\) (joannac_at_btinternetXX.com)
Date: 01/22/04


Date: Thu, 22 Jan 2004 13:32:51 -0000

Dave Fowler wrote:

| This bug has been mentioned before - if you pass a call to a
| constructor of an interfaced object as a const parameter in a
| function call the reference count is not incremented. My questions
| are:
| Has this been fixed in D7 and is there a workaround for D6 other than
| a) never have interfaces as const parameters or b) use a cast like
| Method1(TFred.Create as IFred)

Passing const interface parameters should not increment the refcount; this
is not a bug, it is as it should be.

Your problem is that you are creating an instance of a class in call to a
method with a const parameter; therefore there is no reference to increment
the refcount. The object may live for the duration of the method, but when
it quits the method it, having a refcount of 0 still, will be released.

Until the instance is assigned to a variable the refcount cannot be
incremented. Passing it to a method with a const parameter will produce the
expected results every time.

This is not a bug, it is a case of programmer beware of misunderstanding
interfaces in Delphi :-)

try doing it the right way...

procedure TForm1.Button1Click(Sender: TObject);
var
  aFred: IFred;
begin
  aFred := TFred.Create;
  Method1(aFred);
end;

Now it works as expected.

Joanna

--
Joanna Carter (TeamB)
Consultant Software Engineer
TeamBUG support for UK-BUG
TeamMM support for ModelMaker


Relevant Pages