Solved

Freeing components

Posted on 1998-03-20
3
143 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
ID: 1360664
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
ID: 1360665
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
ID: 1360666
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

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
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…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

803 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