Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it a object or an interface? Do I need to free it or not?

Good day! Function GetService creates instance of class THTTPRIO and returns it as IInvokable. I use this interface/object in another function. Do I have to free it or not when I don't need it anymore? Interfaces does not need to be freed but I'm confused with the fact that RIO is created as object of THTTPRIO class.

function GetService(Addr: string): IInvokable;
var
  RIO: THTTPRIO;
begin
  RIO := THTTPRIO.Create(nil)
  RIO.URL := Addr;
  Result := (RIO as IInvokable);
end;

Where:

IInvokable = interface(IInterface);
THTTPRIO = class(TComponent, IInterface, IRIOAccess);

Thank you in advance! Vojtech

like image 762
Vojtech Avatar asked Mar 14 '12 15:03

Vojtech


People also ask

Is an interface an object?

An interface is a description of the actions that an object can do... for example when you flip a light switch, the light goes on, you don't care how, just that it does. In Object Oriented Programming, an Interface is a description of all functions that an object must have in order to be an "X".

What is the difference between interface and object?

A class can be instantiated i.e, objects of a class can be created. An Interface cannot be instantiated i.e, objects cannot be created. Classes does not support multiple inheritance.

Can you use interface as object?

Like abstract classes, interfaces cannot be used to create objects (in the example above, it is not possible to create an "Animal" object in the MyMainClass) Interface methods do not have a body - the body is provided by the "implement" class. On implementation of an interface, you must override all of its methods.

What is the need to interface?

Why do we use an Interface? It is used to achieve total abstraction. Since java does not support multiple inheritances in the case of class, by using an interface it can achieve multiple inheritances. It is also used to achieve loose coupling.


1 Answers

To answer the question you need to check how IInterface._AddRef and IInterface._Release are implemented for this class. They look like this:

function TRIO._AddRef: Integer;
begin
  Result := TInterlocked.Increment(FRefCount);
end;

function TRIO._Release: Integer;
begin
  Result := TInterlocked.Decrement(FRefCount);
  if (Result = 0) and not (Owner is TComponent) then
    Destroy;
end;

This means that the lifetime of the object is managed by interface reference counting. Which means that your code is correct and does not leak.

Note that if you had passed an Owner in the constructor then the lifetime would be managed by that owner instead.

Your code is still somewhat liable to leaking if the setting of URL raises. I would write it like this:

function GetService(const Addr: string): IInvokable;
begin
  Result := THTTPRIO.Create(nil);
  (Result as IRIOAccess).RIO.URL := Addr;
end;

Having said all of that, the THTTPRIO class does not support IInvokable so perhaps your actual code looks slightly different.

like image 134
David Heffernan Avatar answered Sep 19 '22 00:09

David Heffernan