Interface counting goes wrong when changing parameter values
Original Reporter info from Mantis: carlokok
-
Reporter name: Carlo Kok
Original Reporter info from Mantis: carlokok
- Reporter name: Carlo Kok
Description:
Given the code below.
Result in Delphi:
AddRef:1
AddRef:2
poing
GetOwner1
GetOwner2
Release:1
nil
Release:0
Result in FPC 2.3.1:
AddRef:1
poing
GetOwner1
Release:0
GetOwner2
nil
Release:1436731
An unhandled exception occurred at $0040772E :
EAccessViolation :
$0040772E
An unhandled exception occurred at $004081BC :
Result in FPC 2.2.0:
AddRef:1
poing
GetOwner1
GetOwner2
Release:0
nil
Release:2616307
It doesn't addref/release for non-const interface parameters, causing it to free way too early.
Additional information:
program aIntfTest;
{$APPTYPE CONSOLE}
uses
SysUtils, Classes;
type
IMyIntf = interface
['{34326401-7B67-40FF-8E92-4587F65C8E24}']
function GetOwner: IMyIntf;
procedure Poing;
end;
type
TMYClass = clasS(TObject, IMyIntf)
fRef: Integer;
public
function GetOwner: IMyIntf;
function QueryInterface(const IID: TGUID; out Obj): HRESULT; stdcall;
function _AddRef: Integer; stdcall;
function _Release: Integer; stdcall;
procedure Poing;
end;
{$R *.res}
{ TMYClass }
function TMYClass._AddRef: Integer;
begin
inc(fRef);
result := fRef;
Writeln('AddRef:'+inttostr(result));
end;
function TMYClass._Release: Integer;
begin
Dec(fRef);
result := FRef;
Writeln('Release:'+inttostr(result));
if result = 0 then Free;
end;
function TMYClass.GetOwner: IMyIntf;
begin
Writeln('GetOwner1');
result := nil;
Writeln('GetOwner2');
end;
function TMYClass.QueryInterface(const IID: TGUID; out Obj): HRESULT;
begin
if GetInterface(IID, Obj) then
result := S_OK else result := -1;
end;
var
r: IMyIntf;
procedure Test(x: IMyIntf);
begin
if x <> nil then x.Poing;
x := x.GetOwner;
if x <> nil then x.Poing;
end;
procedure TMYClass.Poing;
begin
writeln('poing');
end;
begin
r := TMYClass.Create;
Test(r);
Writeln('nil');
r := nil;
end.
Mantis conversion info:
- Mantis ID: 10897
- Fixed in version: 2.2.2
- Fixed in revision: 10527 (#8f989ba3)