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

  • SEARCH
  • Home
  • 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 6957013
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T14:56:39+00:00 2026-05-27T14:56:39+00:00

Follow-up question to this question : (note that this is not a duplicate, I’m

  • 0

Follow-up question to this question: (note that this is not a duplicate, I’m asking for alternatives here).

Is there any way to make the following work:

type
  List <T> = record
  private
    FList  : TList <T>;
    FGuard : IInterface,
    procedure CheckCreated;
  public
    procedure Add(const Value : T);
  end;

procedure List <T>.CheckCreated;
begin
if (FGuard = nil) then
  begin
  FList := TList <T>.Create;
  FGuard := TGuard.Create (FList);    // guard calls free on list in destructor
  end;
end;

procedure List <T>.Add (const Value : T);
begin
CheckCreated;
FList.Add (Value);
end;

Ideally I want to use it like this:

function ReturnHandles : List <THandle>;
begin
Result.Add (2);
Result.Add (3);
end;

As explained in the answers to the linked question, this does not work (which is really a pitty). It will not create a new list on each call.

Unfortunately this does not work either:

function ReturnHandles : List <THandle>;
begin
Initialize (Result);
Result.Add (2);
Result.Add (3);
end;

It leaks the guard interfaces and all the lists, because Initialize just overwrites the interface reference without decrementing the reference count.

Is there any way to make this work? Or would you suggest making this an interface instead of a record and just live with the construction line?

function ReturnHandles : List <THandle>;
begin
Result := List <T>.Create;
Result.Add (2);
Result.Add (3);   
end;

Thanks for your help!

  • 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-05-27T14:56:40+00:00Added an answer on May 27, 2026 at 2:56 pm

    This should work fine if I understand you correctly:

    function ReturnHandles : List <THandle>;
    begin
      Finalize(Result);
      Result.Add (2);
      Result.Add (3);
    end;
    

    The Finalize call will ensure that all managed types are set to nil which I believe is your intent.

    This question is very closely related to your previous question and I think that you could make use of out parameters to simplify the code. A function result is implicitly a var parameter, but if you used an explicit out parameter it will initialise the managed types as you desire.

    procedure InitializeHandles(out Handles : List <THandle>);
    begin
      Handles.Add (2);
      Handles.Add (3);
    end;
    

    Personally, since you are introducing an interface into the mix, I think I would be inclined to go all the way and use interfaces exclusively. Or use standard classes and accept the need for try/finally lifetime management.

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

Sidebar

Related Questions

Note that this is a follow-up question from my previous one: How to memorize
I have a follow up question to this one . Now that I have
NOTE: I am not exactly sure how to title or tag this question, so
(Note: This is a follow up to my question Can jQuery.getJSON put a domain's
note: this is a direct follow up to this previous question I have very
This is a follow up question to This Question . I like (and understand)
This is a follow-up question to this one . Take a look at these
This is a follow-up question to this question I asked earlier. Btw thanks Neil
I have a follow-up question to this one . I created a new form,
This is like a follow-up question to this one . Basically what I'm doing

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.