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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T21:28:58+00:00 2026-05-24T21:28:58+00:00

I always thought that the owner is responsible for destroying visual controls and that

  • 0

I always thought that the owner is responsible for destroying visual controls and that I can manually control destruction if I pass nil as the owner.

Consider the following example:

TMyForm = class (TForm)
private
  FButton : TButton;
end;

...
FButton := TButton.Create(nil);   // no owner!!
FButton.Parent := Self;

I would expect this button to produce a memory leak but it doesn’t and in fact the destructor of TButton is called.

Further investigation showed that the TWinControl destructor contains the following snippet of code:

I := ControlCount;
while I <> 0 do
begin
  Instance := Controls[I - 1];
  Remove(Instance);
  Instance.Destroy;
  I := ControlCount;
end;

which looks like it is destroying the child components (the ones with Parent set to the control itself).

I was not expecting the parent control to destroy the control. Can anybody explain why this is happening? And who is destroying the object if I pass in an owner?

  • 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-24T21:29:00+00:00Added an answer on May 24, 2026 at 9:29 pm

    why this is happening?

    It makes sense and it’s by design. What do you think should happen to orphaned child controls when the parent is destroyed? Should they suddenly start floating around as top-level windows? Probably not. Should they be re-parented to another control? Which one?

    who is destroying the object if I pass in an owner?
    

    Parent, if it’s assigned and being freed first. TWinControl overrides TComponent‘s destructor to free its child controls first (the inherited destructor is only called later). The child controls notify their Owner about being destroyed which removes them from its list of owned components. That’s why the Owner doesn’t attempt to free your object again later in its destructor.

    If Parent is the same object as Owner then the above applies, too.

    If Parent and Owner are two different objects, and you free the Owner first, then the Owner component frees all its owned components (see TComponent‘s destructor). Your object is a TControl descendant and TControl overrides the destructor to call SetParent(nil); which removes the instance from the parent’s list of child controls. That’s why the parent doesn’t attempt to free your object again later in its destructor.

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

Sidebar

Related Questions

I have always thought that the .equals() method in java should be overridden to
I have always thought that the terms internationalization and localization (and their funny abbreviations
The comment to this answer got me wondering. I've always thought that C was
a question that a co-interviewer asked in interviews. I always thought it was a
In a database-centric application that is designed for multiple clients, I've always thought it
This is a question that's been nagging me for some time. I always thought
I understand that all InProc session data is always gone when its w3wp owner
I always thought base.Something was equivalent to ((Parent)this).Something , but apparently that's not the
I always thought elements that had display:none CSS style returned 0 for height() and
I've always thought it's the general wisdom that std::vector is implemented as an array,

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.