Solved

Freeing components

Posted on 1998-03-20
3
136 Views
Last Modified: 2010-04-06
Do I have to close free all objects created with Free
before terminating application, or all of them are
automatically free-ed?
0
Comment
Question by:bor061297
3 Comments
 
LVL 2

Accepted Solution

by:
mvz121697 earned 50 total points
Comment Utility
As a general rule:
Free all components which you are creating yourself,
with the Create-method.
So if you are dropping components on your form, don't worry about freeing, Delphi does it for you.

But if you are creating components dynamicly, you have to free them, like this:

var fm:TForm;
begin
fm:=TForm.create(Self);
try
  // stuff...
   fm.ShowModal;
finally  
  fm.Free;
end;


BTW, if you don't follow this rule for VCL-components,
they will still automaticly be freed, when the parent is destroyed. (The parent is the object you supply as a parameter in the create-method).  
You can also let the Application be the parent, like this:

fm:=TForm.Create(Application)

If you don't free this yourself, it will be freed when the application ends.


Greetings,
MvZ




0
 
LVL 4

Expert Comment

by:d003303
Comment Utility
just a little cosmetics, the owner is passed in the Create method. Also, if the owner is freed, all owned components are freed, too. This does not depend on the parent property.
Generally, all components on a form are owned by the form. If e.g. a label resists on a panel wich resists on a form, the owner property of all two components is the form. The parent of the label though is the panel, the parent of the panel is the form. And normally all forms are owned by the application and have no parents (except MDIChild wich has the MDIForm as a parent).

Slash/d003303
0
 

Author Comment

by:bor061297
Comment Utility
If I create a BitMap with:

Bitmap := TBitMap.Create;

is it automatically freed when application ends?
And, who owns Bitmap created in this way
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Suggested Solutions

Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now