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 6564471
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T13:58:15+00:00 2026-05-25T13:58:15+00:00

This is similar to this question . I asked Why? to the most popular

  • 0

This is similar to this question. I asked “Why?” to the most popular response but I don’t know that anyone would ever look at it again. At least not in any timely manner.

Anyway, my question is about best practices for delegating responsibility for creation of objects to functions or procedures, without causing memory leaks. It seems that this:

procedure FillObject(MyObject: TMyObject; SomeParam: Integer);
begin
  //Database operations to fill object
end;

procedure CallUsingProcedure();
var
  MyObject: TMyObject;
begin
  MyObject = TMyObject.Create();
  try
    FillObject(MyObject, 1);
    //use object
  finally
    MyObject.Free();
  end;
end;

is preferred over this:

function CreateMyObject(DBID: Integer): TMyObject;
begin
  Result := TMyObject.Create();
  try
    //Database operations to fill object
  except on E: Exception do
    begin
      Result.Free();
      raise;
    end;
  end;
end;

procedure CallUsingFunction();
var
  MyObject: TMyObject;
begin
  MyObject = CreateMyObject(1);
  try
    //use object
  finally
    MyObject.Free();
  end;
end;

Why?

I’m relatively new to Delphi, having previously worked most with Java and PHP, as well as C++, though to a lesser extent. Intuitively, I lean toward the function method because:

  • It encapsulates the object creation code in the function, rather than create the object separately whenever I want to use the procedure.
  • I dislike methods that alter their parameters. It’s often left undocumented and can make tracing bugs more difficult.
  • Vague, but admittedly it just “smells” bad to me.

I’m not saying I’m right. I just want to understand why the community chooses this method and if there is good reason for me to change.

Edit:
References to @E-Rock in comments are to me(Eric G). I changed my display name.

  • 1 1 Answer
  • 2 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-25T13:58:16+00:00Added an answer on May 25, 2026 at 1:58 pm

    Creating the object instance and passing it into another procedure makes it clear which code is responsible for freeing the instance.

    In the first case (using a procedure to fill it):

    MyObj := TMyObject.Create;
    try
      // Do whatever with MyObj
    finally
      MyObj.Free;
    end;
    

    This is clear that this block of code is responsible for freeing MyObj when it’s finished being used.

    MyObj := CreateMyObject(DBID);
    

    What code is supposed to free it? When can you safely free it? Who is responsible for exception handling? How do you know (as a user of someone else’s code)?

    As a general rule, you should create, use, and free object instances where they’re needed. This makes your code easier to maintain, and definitely makes it easier for someone who comes along later and has to try and figure it out. 🙂

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

Sidebar

Related Questions

There are similar questions to this, but I don't think anyone has asked this
Sorry I know similar question have been asked many times before but like most
I don't think this question has asked yet (most similar questions are about extracting
I realize that there was a similar question asked here , but this is
I have asked a question similar to this in the past but this is
This is similar to a question that has already been asked. However, I am
I already asked a similar question but this one is a bit different/specific: I'm
I asked a similar question before ( integer-vs-char-for-db-record-property ) but stumbled upon something that
This question has already been asked but I am stuck with the most basic
I've asked a similar question before, but I've done some more research and this

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.