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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T03:19:59+00:00 2026-06-12T03:19:59+00:00

In my source I see and use often a construction like: with TSQLDataSet.Create(nil) do

  • 0

In my source I see and use often a construction like:

with TSQLDataSet.Create(nil) do try
  //Dosomething, get a result from a query.    
finally
  Free;
end;

But I also often see the construction:

with TSQLDataSet.Create(nil) do begin
  //Dosomething, get a result from a query.    
end;

If I create objects with a with contruction, do I need to free them or is that automatic at the end of the with-block?

  • 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-12T03:20:01+00:00Added an answer on June 12, 2026 at 3:20 am

    You need to free.

    Because Class.Create is just an expression, and Delphi cannot know which expression the resulting value came from.

    You can just read VCL sources – there is always explicit .Free.

    And you can think of non-object example:

    var r: record .... end;
    with r do begin
    ...
    end;
    

    If Delphi tried to free everything at with end – then it would try to free non-object ?

    with SomeObjectFactory.GetMeAnObject do begin
    ...
    end;
    

    Here you create object not by constructor, but by some function. And this function is not different from TLabel.Font or TDataSet.FieldByName.
    Should Delphi free it or not here ?

    To avoid the risky guesswork and keep it uniform – with is just with. It is just an alias. Nothing more. It was designed back in 1974 and is does not clone functionality of latest .Net/Scala Using(x){..} constructs.


    What you maybe saw were constructs like

    with TForm.Create(Application) do ...;
    with TLabel.Create(MainForm.Panel1) do ...;
    

    That is very different – that does insert newly created control as belonging to an owner. The owner would free all its owned components when it is freed itself. But that does not use Create(nil). And when it still does – then inside the with block you would see some explicit call, binding the object to some container/parent (though that is very fragile in case of exceptions between .Create and binding).

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

Sidebar

Related Questions

I use gitweb. In the tree view I see my source code. But this
I want to see the source of functions like ns_initparse(), res_search() etc. Where can
See the fiddle: http://jsfiddle.net/stefek99/9m5NZ/1/ Get the source: http://pastie.org/3654715 Chrome 17 : just works. IE
Quite often I see source code where language's keyword are replaced with full type
I often see in source codes the usage of if (object.ReferenceEquals(myObject, null)) for checking
When reading some FreeBSD source code (See: radix.h lines 158-173), I found variable declarations
First see my Source Code: WebClient client = new WebClient(); String url = http://localhost;
I want see the source code of System.Web.Routing.RouteValueDictionary class. But Reflector can't disassemble it
I can't see my source file in the scripts list, but I can see
I want the user to be able to see the source code of the

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.