Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8501013
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T01:05:43+00:00 2026-06-11T01:05:43+00:00

I’m trying of execute a code like this: IFoo = Interface procedure DoFoo; end;

  • 0

I’m trying of execute a code like this:

IFoo = Interface
   procedure DoFoo;
end;

TFoo = class (TInterfaceObject, IFoo)
  .....
   procedure DoFoo;
end;

TFoo2 = class (TInterfaceObject)
  ......
end;

TGenClass = class
  class function DoSomething<T: class, cronstructor>: T;
end;

class function TGenClass.DoSomething<T>: T;
var Obj: T;
    Foo: IFoo;
begin
   Obj := T.Cretae;
   if Obj.GetInterfaceEntry(IFoo) <> nil then 
   begin
     Obj.GetInterface(IFoo, Foo);
     Foo.DoFoo;
   end;
   result := Obj;
end; 
......
var f: TFoo;
    f2: TFoo2; 
begin
  f:= TGenClass.DoSomeThing<TFoo>;
  f2:= TGenClass.DoSomeThing<TFoo2>;
  f2.free;
  f.free;
end;

When I execute this code, f.free raise a exception, because is already free, I suppose, because if I comment this lines

Obj.GetInterface(IFoo, Foo);
Foo.DoFoo;

it work.

¿How can execute IFoo interface without free object?

thk.

ADD:

Thanks all. I understand.

I tried to return IFoo with same result. My problem is that T could not be TInterfacedObject. The Java code I trying to convert is:

public  void dataIterate(int recNo, ResultSet data) throws SQLException {

    try {

        Constructor c = itemClass.getDeclaredConstructor();
        c.setAccessible(true);
        Object item = c.newInstance();
        if (item instanceof CustomInitialize) ((CustomInitialize)item).initialize(data);
        else {

            if (metadata == null ) metadata = data.getMetaData();
            for (int i=1; i<= metadata.getColumnCount(); i++)
                assignProperty(itemClass, item, "set"+metadata.getColumnName(i).toLowerCase(), data.getObject(i));

        }
        add((E)item);

    } catch (Exception ex) {
        throw new SQLDataException(ex);
    }
   ..........

Delphi example code:

program Project4;

{$APPTYPE CONSOLE}

{$R*.res}

uses
   System.SysUtils,  System.Rtti;

type
  IFoo = Interface
     ['{F2D87AE6-1956-4B82-A28F-DC011C529849}']
     procedure DoFoo;
  end;

  TFoo = class (TInterfacedObject, IFoo)
  private
    FName: String;
  public
     procedure DoFoo;
     property Name: String Read FName write FName;
  end;

  TFoo2 = class (TObject)
  private
    FName: String;
  published
      property Name: String Read FName write FName;
  end;

TGenClass = class
  class function DoSomething<T: class, constructor>: T;
end;

class function TGenClass.DoSomething<T>: T;
var Obj: T;
    Foo: IFoo;
    Ap: TRttiProperty;
    Ctx: TRttiContext;
begin
   Obj := T.Create;
   if Obj.GetInterfaceEntry(IFoo) <> nil then
   begin
     Obj.GetInterface(IFoo, Foo);
     Foo.DoFoo;
   end;
   Ctx.GetType(TypeInfo(T)).GetProperty('Name').SetValue(TObject(Obj),'AName');
   result := Obj;
end;
{ TFoo }

procedure TFoo.DoFoo;
begin
  writeln('Foo executed.');
end;
var f: TFoo;
    f2:TFoo2;
begin
  try
    f:= TGenClass.DoSomeThing<TFoo>;
    f2:= TGenClass.DoSomeThing<TFoo2>;
    writeln(f2.Name);
    writeln(f.Name);   //<-- raise exception
    f.free;
    f2.Free;
    readln;

  except
    on E: Exception do
      Writeln(E.ClassName, ': ', E.Message);
  end;
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T01:05:44+00:00Added an answer on June 11, 2026 at 1:05 am

    Let’s take a look at this code:

    class function TGenClass.DoSomething<T>: T;
    var Obj: T;
        Foo: IFoo;
    begin
       Obj := T.Create;
       if Obj.GetInterfaceEntry(IFoo) <> nil then 
       begin
         Obj.GetInterface(IFoo, Foo);
         Foo.DoFoo;
       end;
       result := Obj;
    end; 
    

    After Obj := T.Create, the object has a reference count of zero, because no interface variable has yet referenced it. Then you call GetInterface and take an interface reference in Foo. So the object now has a reference count of 1. Then the function returns and Foo goes out of scope. This reduces the reference count to 0 and so the object is freed.

    When you use TInterfacedObject, you must always hold an interface variable. So that the reference counting can manage the object’s life. In this case you have mixed object references and interface variables and that invariably leads to pain and anguish.

    I can’t really advise you on what your code should look like because I don’t know what your problem is. All I have attempted to do is to explain the behaviour for you. Perhaps DoSomething should be returning IFoo rather than T. Or perhaps you need to stop using reference counted lifetime management. Very hard to be sure from here.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
For some reason, after submitting a string like this Jack’s Spindle from a text
I am trying to render a haml file in a javascript response like so:
I have this code to decode numeric html entities to the UTF8 equivalent character.
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I would like to count the length of a string with PHP. The string

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.