10

First, a little explanation about my situation:

I have a sample interface which is implemented by different classes, and these classes might not always have a shared ancestor:

  IMyInterface = interface
    ['{1BD8F7E3-2C8B-4138-841B-28686708DA4D}']
    procedure DoSomething;
  end;

  TMyImpl = class(TInterfacedPersistent, IMyInterface)
    procedure DoSomething;
  end;

  TMyImp2 = class(TInterfacedObject, IMyInterface)
    procedure DoSomething;
  end;

I also have a factory method which is supposed to create an instance of an object which implements my interface. My factory method receives the class name as its parameter:

  function GetImplementation(const AClassName: string): IMyInterface;

I tried two approaches to implement this factory method, the first one was using extended RTTI:

var
  ctx : TRttiContext;
  t : TRttiInstanceType;
begin
  t := ctx.FindType(AClassName).AsInstance;
  if Assigned(t) then
    Result := t.GetMethod('Create').Invoke(t.MetaclassType, []).AsInterface as IMyInterface;
end;

In this approach I am calling the default constructor which is fine in my scenario. The problem with this is, at runtime, I get an error telling me the object does not support IMyInterface. What's more, the created object is not assigned to an interface variable; therefore, it will be leaked. I also tried returning the value using TValue.AsType method, but it gives me Access Violation:

function GetImplementation(const AClassName: string): IMyInterface;
var
  ctx : TRttiContext;
  rt : TRttiInstanceType;
  V : TValue;
begin
  rt := ctx.FindType(AClassName).AsInstance;
  if Assigned(rt) then
  begin
    V := rt.GetMethod('Create').Invoke(rt.MetaclassType, []);
    Result := V.AsType<IMyInterface>;
  end;
end;

.

The second approach I tried was using a generic dictionary to hold pairs of , and provide registration, unregistration methods:

  TRepository = class
  private
    FDictionary : TDictionary<string, TClass>;
  public
    constructor Create;
    destructor Destroy; override;
    function GetImplementation(const AClassName: string): IMyInterface;
    procedure RegisterClass(AClass: TClass);
    procedure UnregisterClass(AClass: TClass);
  end;

Here I implemented GetImplementation method as this:

function TRepository.GetImplementation(const AClassName: string): IMyInterface;
var
  Obj : TObject;
begin
  if FDictionary.ContainsKey(AClassName) then
  begin
    Obj := FDictionary[AClassName].Create;
    Obj.GetInterface(IMyInterface, Result);
  end;
end;

This works fine, and I can call DoSomething method using the returned value of GetImplementation, but it still has the memory-leak problem; Obj which is created here is not assigned to any interface variable; therefore, it is not reference-counted, and is leaked.

.

Now, my actual question:

So my question is, how can I safely create an instance of a class which implements my interface at runtime? I saw Delphi Spring Framework, and it provides such functionality in its Spring.Services unit, but it has its own reflection routines and lifetime management models. I am looking for a lightweight solution, not a whole 3rd-party framework to do this for me.

Regards

RRUZ
  • 134,889
  • 20
  • 356
  • 483
vcldeveloper
  • 7,399
  • 2
  • 33
  • 39
  • 2
    I bet that the answer is that once you had everything you need, you have the Spring framework, or a homomorphism that maps cleanly onto it. Lisp coders often find that they "discover" the right answer, almost like scientists uncovering the order of the physical universe. In that sense, I suspect you will rediscover england (Spring). – Warren P Nov 16 '11 at 21:30
  • 2
    Just one note: The constructor is not always called "Create", so it might be more robust to loop through the methods using Rtti and look for a parameterless constructor. – jpfollenius Nov 16 '11 at 21:56

3 Answers3

11

The first case using the RTTI give you a access violation because the TRttiContext.FindType(AClassName) cannot find the Rtti info for the classes which are not registered or used explicity in the app.

So you can change your code to

function GetImplementation(AClass: TClass): IMyInterface;
var
  ctx : TRttiContext;
  t : TRttiInstanceType;
begin
  t := ctx.GetType(AClass).AsInstance;
  if Assigned(t) then
    Result := t.GetMethod('Create').Invoke(t.MetaclassType, []).AsInterface As IMyInterface;
end;

and call in this way

AClass:=GetImplementation(TMyImp2);

Now if you want to use the Class name to invoke the class, using a list (like your TRepository class) to register the classes is a valid aproach. about the memory leak i'm pretty sure which is caused because the TMyImpl class is derived from the TInterfacedPersistent which not implement reference counting directly like the TInterfacedObject.

This implementation of the the TRepository must works ok.

constructor TRepository.Create;
begin
  FDictionary:=TDictionary<string,TClass>.Create;
end;

destructor TRepository.Destroy;
begin
  FDictionary.Free;
  inherited;
end;

function TRepository.GetImplementation(const AClassName: string): IMyInterface;
var
  Obj : TObject;
begin
  if FDictionary.ContainsKey(AClassName) then
  begin
    Obj := FDictionary[AClassName].Create;
    Obj.GetInterface(IMyInterface, Result);
  end;
end;

{
or using the RTTI
var
  ctx : TRttiContext;
  t : TRttiInstanceType;
begin
  t := ctx.GetType(FDictionary[AClassName]).AsInstance;
  if Assigned(t) then
    Result := t.GetMethod('Create').Invoke(t.MetaclassType, []).AsInterface As IMyInterface;
end;
}

procedure TRepository.RegisterClass(AClass: TClass);
begin
  FDictionary.Add(AClass.ClassName,AClass);
end;

procedure TRepository.UnregisterClass(AClass: TClass);
begin
  FDictionary.Remove(AClass.ClassName);
end;
RRUZ
  • 134,889
  • 20
  • 356
  • 483
  • If you have the class type, you can just call `TClassType.Create`. Why would you use Rtti if you know the class type? – jpfollenius Nov 16 '11 at 21:54
  • I'm not using the RTTI, the Rtti code is disabled and only is showed as an example. – RRUZ Nov 16 '11 at 21:56
  • 1
    From now on I will be using `StrList := Context.GetType(TStringList).GetMethod('Create').Invoke(Context.GetType(TStringList).MetaclassType, []).AsObject as TStringList;` :) – jpfollenius Nov 16 '11 at 21:59
  • 2
    +1 I didn't spot `TInterfacedPersistent`. That's the memory leak right there. – David Heffernan Nov 16 '11 at 21:59
  • @RRUZ: in your first paragraph you suggest changing the function so that it takes the class type. This does not make sense IMHO. Or did I get you wrong? – jpfollenius Nov 16 '11 at 22:00
  • Ahh, ok now I understeand, yeah in this case for call the constructor use the RTTI not make sense, but when you need call another method is very useful. Also the first paragraph is just part of the whole answer. :) – RRUZ Nov 16 '11 at 22:09
  • @PRUZ, the access violation is not caused by a nil result from FindType method. I am aware about using fully qualified class names and registering the classes. Actually that's why I used TInterfacedPersist as the ancestor class; cuz RegisterClass function does not accept classes inherited from TInterfacedObject. – vcldeveloper Nov 17 '11 at 01:37
  • I was not aware it does not implement reference-counting itself, and as you said, that was causing the memory leak. Now that I found why the memory is leaked, I changed the ancestor to TInterfacedObject, and the second approach (using a dictionary lookup) works just fine, so I am going with that, abandoning RTTI for this. Thank you! – vcldeveloper Nov 17 '11 at 01:39
  • 1
    Glad to help you :) and FYI the nickname is RRUZ (Rodrigo Ruz) not PRUZ ;) – RRUZ Nov 17 '11 at 01:41
  • Awww, so sorry! it might seem funny, but all these years I read your blog or your forum posts, I always read it as PRUZ, until now that you mentioned it! I think that's probably cuz we have a similar (to PRUZ) male name in Persian :) – vcldeveloper Nov 17 '11 at 02:14
4

I think I would opt for the second option, mainly because I prefer to avoid RTTI unless it is the only possible solution to a problem.

But in both your proposed options you state that

the object which is created here is not assigned to any interface variable

That's simply not true. In both cases you assign to Result which has type IMyInterface. If you have a memory leak, it is caused by some other code, not by this code.

And @RRUZ has found the cause of the leak – namely using TInterfacedPersistent which does not implement reference counted lifetime management. Your code won't leak for TInterfacedObject.

For what it is worth, I would assign directly to the interface variable rather than via an object reference, but that is just a matter of stylistic preference.

if FDictionary.TryGetValue(AClassName, MyClass) then
  Result := MyClass.Create as IMyInterface;
David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
  • The leaked object was of the same type of the created object in GetImplementation method, that's why I thought that is just a dangling object which is not assigned to any interface. The problem as you mentioned, was in using TInterfacedPersistent. I was not aware it does not implement reference-counting itself, and I used it just to be able to register my classes to be accessed using RTTI. – vcldeveloper Nov 17 '11 at 01:34
  • Regarding your suggestion about using direct interface assignment; that works when I have the specific class type, but my dictionary is storing just a reference to TClass; therefore, if I use your code, compiler will complain that "Operator not applicable to this operand type", but retrieving the interface using an object reference works around that nag. – vcldeveloper Nov 17 '11 at 01:46
  • The problem here is that TClass does not implement IInterface which is needed for the "as" operator (see here: [link](http://stackoverflow.com/questions/14931940/casting-object-to-interface-type-with-no-tinterfacedobject-as-base-class). Defining a new type TIntfClass = class of TInterfacedObject would help. – sausagequeen Apr 02 '14 at 11:03
2

You can do it using extended RTTI and TObject's GetInterface method:

function GetImplementation(const AClassName: string): IMyInterface;
var
  ctx: TRttiContext;
  t : TRttiInstanceType;
  obj: TObject;
begin
  Result := nil;
  t := ctx.FindType(AClassName).AsInstance;
  if Assigned(t) then begin
    obj := t.GetMethod('Create').Invoke(t.MetaclassType, []).AsObject;
    obj.GetInterface(IMyInterface, Result)
  end;
end;

It won't work if the object overrides QueryInterface to do custom processing, but both TInterfacedPersistent and TInterfacedObject rely on GetInterface.

Zoë Peterson
  • 13,094
  • 2
  • 44
  • 64