Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2004-04

no refcounted interfaces

2 messages · 2 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Dean Zobec
I've talked with some friends of mine about the use of Interface base classes
without reference counting in Delphi
like this
type
  TNoRefCount = class(TObject, IUnknown)
  protected
    function QueryInterface(const IID:TGUID; out Obj):HResult; stdcall;
    function _AddRef: Integer; stdcall;
    function _Release: Integer; stdcall;
  end;
function TNoRefCount.QueryInterface(const IID:TGUID; out Obj): HResult;
begin
  if GetInterface(IID, Obj) then
    Result := 0
  else
    Result := Windows.E_NoInterface;
end;
function TNoRefCount._AddRef: Integer;
begin
  Result := -1
end;
function TNoRefCount._Release: Integer;
begin
  Result := -1
end;

We are all experiencing the same kind of problems:

1) there's no easy way to obtain a TObject from an interface (whereas there's no particular reason not to have it. A solution proposed was to implement an AsObject method in all the interfaces used.
2) there's no FreeAndNil function that would free the interface and the object instance.

It would be nice to be able to do something like this in an abstract factory:
var
  MyRef: ISomeInterface;
begin
  MyRef := SomeFactory.CreateObject as ISomeInterface;
  try
    ...use MyRef...
  finally
    MyRef.AsObject.Free;
    MyRef := nil;
  end;
end;

Any Idea of a solution in free pascal?

Ciao, Dean

Florian Klaempfl
to Dean Zobec
Dean Zobec wrote 52 lines
I've talked with some friends of mine about the use of Interface base classes without reference counting in Delphi like this type TNoRefCount = class(TObject, IUnknown) protected function QueryInterface(const IID:TGUID; out Obj):HResult; stdcall; function _AddRef: Integer; stdcall; function _Release: Integer; stdcall; end; function TNoRefCount.QueryInterface(const IID:TGUID; out Obj): HResult; begin if GetInterface(IID, Obj) then Result := 0 else Result := Windows.E_NoInterface; end; function TNoRefCount._AddRef: Integer; begin Result := -1 end; function TNoRefCount._Release: Integer; begin Result := -1 end; We are all experiencing the same kind of problems: 1) there's no easy way to obtain a TObject from an interface (whereas there's no particular reason not to have it. A solution proposed was to implement an AsObject method in all the interfaces used. 2) there's no FreeAndNil function that would free the interface and the object instance. It would be nice to be able to do something like this in an abstract factory: var MyRef: ISomeInterface; begin MyRef := SomeFactory.CreateObject as ISomeInterface; try ...use MyRef... finally MyRef.AsObject.Free; MyRef := nil; end; end; Any Idea of a solution in free pascal?

From the fpc wiki
(http://www.freepascal.org/wiki/wiki.phtml?title=Language_related_articles)

Interface type selection: The commandline switch -SI and the directive $interfaces (possible values: corba and com) allow you to choose the type of interfaces which has no parent. Interfaces with parent inherit the style from their parent regardless of the currently selected interface type.

* COM style interfaces work like Delphi interfaces: they inherit implicitly from IUnknown and they are reference counted.
* CORBA style interfaces are neither ref. counted nor do they inherit from IUnknown.