[fpc-pascal] Problem with interfaces

Joao Morais post at joaomorais.com.br
Mon Aug 20 03:51:22 CEST 2007


Hello,

I have the following code:

{$mode objfpc}
interface

...

function DefaultObj: TObject;

implementation

var
   _Holder: IHolder;

function DefaultObj: TObject;
begin
   if not Assigned(_Holder) then
     _Holder := THolder.Create(TSomeClass.Create);
   Result := _Holder.Instance;
end;

IHolder is an interface that the THolder class implements. The first 
call to the DefaultObj function creates the holder instance, as 
expected. New calls to the same function doesn't recreate it. Again, as 
expected.

The first call that I make to DefaultObj from *another* unit recreate 
the object, because, I don't know why, the _holder var points again to nil.

Changing the type of the _holder to THolder (ie an object pointer 
instead of an interface pointer) everything works flawlessly.

I have tried to create a small sample but couldn't reproduce the issue, 
anyway the source of the problem is gpl code. It fails under 2.0.4 and 
current fixes_2_2 branch. Hints?

Thanks.

--
Joao Morais




More information about the fpc-pascal mailing list