Progam crash with usage of unit global interfaces variables (Tested: Windows : fpc 2.2.5 / FreeBSD 2.3.1)
Original Reporter info from Mantis: SirHelly
-
Reporter name: H.Hartl
Original Reporter info from Mantis: SirHelly
- Reporter name: H.Hartl
Description:
Problems with global usage of interfaces.
------
When building a pluggable interface design, with strong separation of implementation
code and interface design it should not be necessary that the program using the plugged
interface needs to "USE" / include the implementing unit. The problem does not occur if
the interface Variable is not global, e.g. returned by a factory method.
Steps to reproduce:
- Generate three source files:
- Source 1: &LtPos;iftest.pas>
program iftest;
{$mode DELPHI}
uses UnitIF_TestC;
begin
Writeln('START');
GTEST.Test;
Writeln('END');
end.
- Source 2: &LtPos;unitif_testb.pas>
unit UnitIF_TestB;
{$mode DELPHI}
interface
uses UnitIF_TestC;
implementation
type
TTestIntf=class(TInterfacedObject,ITestIF)
procedure Test;
end;
procedure TTestIntf.Test;
begin
writeln('OK');
end;
initialization
GTEST:=TTestIntf.Create;
writeln('ASSIGNED IF');
end.
- Source 3: &LtPos;unitif_testc.pas>
unit UnitIF_TestC;
{$mode DELPHI}
interface
type ITestIF=interface
procedure Test;
end;
var GTEST:ITestIF;
implementation
uses UnitIF_TestB;
end.
- Compile with: fpc -B -gl iftest.pas
- run program iftest.exe
c:\SVNNG\FG4\TESTS>iftest.exe
ASSIGNED IF
START
Runtime error 216 at $004013C2
$004013C2 main, line 6 of iftest.pas
$00407DF0
-
the expected behaviour should be:
c:\SVNNG\FG4\TESTS>iftest.exe
ASSIGNED IF
START
OK
END -
Workaround:
program IFTest;
{$mode DELPHI}
uses UnitIF_TestB,UnitIF_TestC; //<<< Add Implemeting UNIT !! Order depends!
//uses UnitIF_TestC,UnitIF_TestB; //<<< gives Runtime error 216
begin
Writeln('START');
GTEST.Test;
Writeln('END');
end.
- The workaround (5) should not be necessary as it compromises the design goal.
Mantis conversion info:
- Mantis ID: 13345
- Version: 2.2.5
- Fixed in version: 2.4.0
- Fixed in revision: 13042 (#e2b8792b)
- Monitored by: » SirHelly (H.Hartl)