Watch, Follow, &
Connect with Us

Please visit our new home
community.embarcadero.com.


Welcome, Guest
Guest Settings
Help

Thread: Are objects freed when closing the main form?


This question is not answered. Helpful answers available: 2. Correct answers available: 1.


Permlink Replies: 2 - Last Post: Sep 18, 2017 11:16 AM Last Post By: Peter Below Threads: [ Previous | Next ]
Toby Dobbs

Posts: 77
Registered: 9/24/16
Are objects freed when closing the main form?  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Sep 18, 2017 4:08 AM
Hi,

I would like to know whether objects created during run-time are automatically freed when the main form closes? I would have presumed that the application's heap would be freed at this point by the OS but I have seen code where a number of objects are freed programatically when the form closes. any explanation/clarification would be appreciated.
Paul TOTH

Posts: 196
Registered: 1/2/17
Re: Are objects freed when closing the main form?  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Sep 18, 2017 4:23 AM   in response to: Toby Dobbs in response to: Toby Dobbs
Le 18/09/2017 à 13:08, Toby Dobbs a écrit :
Hi,

I would like to know whether objects created during run-time are automatically freed when the main form closes? I would have presumed that the application's heap would be freed at this point by the OS but I have seen code where a number of objects are freed programatically when the form closes. any explanation/clarification would be appreciated.

an object can be freed by:

1) it's owner for a TComponent created with an Owner <> nil
2) it's Parent for a TControl put on a Parent
3) by code

components put on a Form at Design time are created with both an Owner
and a Parent (but they're freed only once :) )

on NEXTGEN (Mobile and Linux) the ARC release also the objects

procedure TForm1.FormCreate(Sender: TObject);
begin
with TButton.Create(nil) do
begin
Caption := 'No parent, no owner'; // never released
end;

with TButton.Create(nil) do
begin
Parent := Self;
Caption := 'No owner'; // released by it's parent
end;

with TButton.Create(Self) do
begin
Caption := 'No parent'; // released by it's owner
end;

with TButton.Create(Self) do
begin
Parent := Self;
Caption := 'With parent and owner'; // released by it's parent
end;

end;

Peter Below

Posts: 1,227
Registered: 12/16/99
Re: Are objects freed when closing the main form?  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Sep 18, 2017 11:16 AM   in response to: Toby Dobbs in response to: Toby Dobbs
Toby Dobbs wrote:

Hi,

I would like to know whether objects created during run-time are
automatically freed when the main form closes? I would have presumed
that the application's heap would be freed at this point by the OS
but I have seen code where a number of objects are freed
programatically when the form closes. any explanation/clarification
would be appreciated.

As Paul Toth said in his reply TComponent descendents that have an
Owner are freed by the owner, which covers anything you drop on a form,
frame, or datamodule at design-time, and autocreated forms or
datamodules. TControl-descendents created in code with Nil as the owner
but parented to a TWincontrol descendent (in code) are freed by the
Parent.

Any other type of object you create in code (unless you do it in a
project compiled for a mobile platform, or its lifetime is controlled
by interface reference counting) is not automatically freed, you have
to do that at an appropriate place in code as well. The memory used by
the application (stack, heap etc.) is returned to the OS when the
application ends, but any object not explicitely freed would not call
its destructor. In most cases that would do no harm (since the Windows,
for example, automatically closes all API handles the process may still
have open when the process ends), but it may cause problems if the
object is using an external resource that needs to be released in a
controlled manner.

--
Peter Below
TeamB
Legend
Helpful Answer (5 pts)
Correct Answer (10 pts)

Server Response from: ETNAJIVE02