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
To answer the question you need to check how
IInterface._AddRefandIInterface._Releaseare implemented for this class. They look like this: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
Ownerin the constructor then the lifetime would be managed by that owner instead.Your code is still somewhat liable to leaking if the setting of
URLraises. I would write it like this:Having said all of that, the
THTTPRIOclass does not supportIInvokableso perhaps your actual code looks slightly different.