Board index » delphi » Q: Action := caFree

Q: Action := caFree

I've just been experimenting with "Action := caFree" in a secondary
forms OnClose event, and it doesn't seem to have any effect ?

I am using Delphi 1 and Windows 3.11 for Workgroups. The Windows
Program Manager's "Help/About Program Manager..."  indicates the
percentage of free system resources. When I call  "Form2.ShowModal",
there is a 2 percent decrease in the available system resources, which
is due to the use of two StringGrids on this form. When I close the
form with "Action := caFree", the 2 percent system resources are not
returned. Form2 was removed from the list of autocreated forms is
created with "Application.CreateForm()" just before it is shown; I had
hoped that the call to "caFree" would destroy the form and free all
the resources allocated to it, but this isn't happening ?

Although I have a growing collection of Delphi books, I have been
unable to find any real info on the subject of dynamic creation of
forms; the Internet in this instance has also provided no insight.

My reason for wanting to create (and destroy ?) the above form, is to
reduce unecessary use of sytem resources; the form is not referenced
by any others at any time. Can anyone suggest why "caFree" isn't
working, and what would be the correct way to destroy a form, so that
everything if freed ?

Thanks !

Simon R Knight

 

Re:Q: Action := caFree


On Sat, 03 Jan 1998 03:58:03 GMT, s...@tcp.co.uk (Simon R Knight)
wrote:

Quote
>I've just been experimenting with "Action := caFree" in a secondary
>forms OnClose event, and it doesn't seem to have any effect ?

CaFree works, so that is not the source of the problem.

Quote
>I am using Delphi 1 and Windows 3.11 for Workgroups. The Windows
>Program Manager's "Help/About Program Manager..."  indicates the
>percentage of free system resources. When call  "Form2.ShowModal",
>there is a 2 percent decrease in the available system resources, which
>is due to the use of two StringGrids on this form. When I close the
>form with "Action := caFree", the 2 percent system resources are not
>returned. Form2 was removed from the list of autocreated forms is
>created with "Application.CreateForm()" just before it is shown; I had
>hoped that the call to "caFree" would destroy the form and free all
>the resources allocated to it, but this isn't happening ?

The important issue isn't whether a single use of the form reduces
resources, but whether repeated use of the form repeatedly decreases
available resources. That indicates a true resource leak.

Using caFree is correct. If you have a resource leak, you must look
elsewhere for the cause. But first make sure you really have a leak.
--
Ray Lischner (http://www.tempest-sw.com/)
Author of "Hidden Paths of Delphi 3: Experts, Wizards, and the Open Tools API"

Re:Q: Action := caFree


Quote
On Sat, 03 Jan 1998 19:28:24 GMT, n...@junk.mail (Ray Lischner) wrote:
>On Sat, 03 Jan 1998 03:58:03 GMT, s...@tcp.co.uk (Simon R Knight)
>wrote:

>>I've just been experimenting with "Action := caFree" in a secondary
>>forms OnClose event, and it doesn't seem to have any effect ?

>CaFree works, so that is not the source of the problem.

>>I am using Delphi 1 and Windows 3.11 for Workgroups. The Windows
>>Program Manager's "Help/About Program Manager..."  indicates the
>>percentage of free system resources. When call  "Form2.ShowModal",
>>there is a 2 percent decrease in the available system resources, which
>>is due to the use of two StringGrids on this form. When I close the
>>form with "Action := caFree", the 2 percent system resources are not
>>returned. Form2 was removed from the list of autocreated forms is
>>created with "Application.CreateForm()" just before it is shown; I had
>>hoped that the call to "caFree" would destroy the form and free all
>>the resources allocated to it, but this isn't happening ?

>The important issue isn't whether a single use of the form reduces
>resources, but whether repeated use of the form repeatedly decreases
>available resources. That indicates a true resource leak.

>Using caFree is correct. If you have a resource leak, you must look
>elsewhere for the cause. But first make sure you really have a leak.

Re:Q: Action := caFree


Quote
On Sat, 03 Jan 1998 19:28:24 GMT, n...@junk.mail (Ray Lischner) wrote:
>On Sat, 03 Jan 1998 03:58:03 GMT, s...@tcp.co.uk (Simon R Knight)
>wrote:

>>I've just been experimenting with "Action := caFree" in a secondary
>>forms OnClose event, and it doesn't seem to have any effect ?

>CaFree works, so that is not the source of the problem.

Since posting my article, I have had a couple of responses which have
said that "Action := caFree" doesn't actually work, or that it doesn't
work with modal forms ?

You are correct in that the form and it's two StringGrids were not
responsible for the 2 percent drop in resources, but rather a coding
oversight of mine. The subject of resources is still somewhat vague to
me, as very little seems to be written on this subject for Delphi. It
would be good if there was a table of some kind that indicated the
amount of resources that are typically allocated for different
controls. I thought that the StringGrid components must have been
using a lot, but this doesn't appear to be true.

Quote
>>I am using Delphi 1 and Windows 3.11 for Workgroups. The Windows
>>Program Manager's "Help/About Program Manager..."  indicates the
>>percentage of free system resources. When call  "Form2.ShowModal",
>>there is a 2 percent decrease in the available system resources, which
>>is due to the use of two StringGrids on this form. When I close the
>>form with "Action := caFree", the 2 percent system resources are not
>>returned. Form2 was removed from the list of autocreated forms is
>>created with "Application.CreateForm()" just before it is shown; I had
>>hoped that the call to "caFree" would destroy the form and free all
>>the resources allocated to it, but this isn't happening ?

>The important issue isn't whether a single use of the form reduces
>resources, but whether repeated use of the form repeatedly decreases
>available resources. That indicates a true resource leak.

But what about a situation where a secondary form is covered with many
components and consumes significant resources; isn't it important that
those resources can be freed if they are no longer required. In this
context it certainly appears that a form should reduce resources when
"Action := caFree" is called.

Thanks for your comments,

Simon R Knight

Re:Q: Action := caFree


In article <34b1b7b8.1617...@news.tcp.co.uk>,
s...@tcp.co.uk (Simon R Knight) wrote:

Quote

>I've just been experimenting with "Action := caFree" in a secondary
>forms OnClose event, and it doesn't seem to have any effect ?

>I am using Delphi 1 and Windows 3.11 for Workgroups. The Windows
>Program Manager's "Help/About Program Manager..."  indicates the
>percentage of free system resources. When I call  "Form2.ShowModal",
>there is a 2 percent decrease in the available system resources, which
>is due to the use of two StringGrids on this form. When I close the
>form with "Action := caFree", the 2 percent system resources are not
>returned. Form2 was removed from the list of autocreated forms is
>created with "Application.CreateForm()" just before it is shown; I had
>hoped that the call to "caFree" would destroy the form and free all
>the resources allocated to it, but this isn't happening ?

>Although I have a growing collection of Delphi books, I have been
>unable to find any real info on the subject of dynamic creation of
>forms; the Internet in this instance has also provided no insight.

>My reason for wanting to create (and destroy ?) the above form, is to
>reduce unecessary use of sytem resources; the form is not referenced
>by any others at any time. Can anyone suggest why "caFree" isn't
>working, and what would be the correct way to destroy a form, so that
>everything if freed ?

I've been having the same problems using Delphi 3.
Memory Sleuth reports that when I use and MDI child form and removes it
using caFree it doesn't destroy the window resources. Every time I open and
close a new MDI child there is a resource leak.
I never got any real solutions in the Delphi groups.
But the workaround I'm using is to manually release every component on the
form. It works, but shouldn't really be necessary..

   i:= ComponentCount - 1;
   while i >= 0 do begin
     if Components[i] is TWinControl then (Components[i] as TWinControl).free;
     dec(i);
   end;

--
|| Bjorn K. Nilssen      // http://home.sol.no/~bjoernk/  // mainly old 3D ||
|| Grimsvollen 30       //
|| N-4616 Kristiansand //
|| NORWAY             //

Re:Q: Action := caFree


Ray Lischner <n...@junk.mail> wrote in article
<34b490bb.136135...@news.proaxis.com>...

Quote
> On Sat, 03 Jan 1998 03:58:03 GMT, s...@tcp.co.uk (Simon R Knight)
> wrote:

> >I've just been experimenting with "Action := caFree" in a secondary
> >forms OnClose event, and it doesn't seem to have any effect ?

> CaFree works, so that is not the source of the problem.

<snip>

I also confirmed to myself that setting Action to caFree on Modal forms
does appear to have a leak.  Since I confirmed this in Delphi 1, the only
time I have used caFree was with modeless MDI children...

HTH,

--
Conor
     co...@globalgateway.co.uk

Re:Q: Action := caFree


In article <01bd19f6$4c118df0$d950dfc2@conors>,

Quote
"Conor Boyd" <co...@nospam.globalgateway.co.uk> wrote:
>Ray Lischner <n...@junk.mail> wrote in article
><34b490bb.136135...@news.proaxis.com>...
>> On Sat, 03 Jan 1998 03:58:03 GMT, s...@tcp.co.uk (Simon R Knight)
>> wrote:

>> >I've just been experimenting with "Action := caFree" in a secondary
>> >forms OnClose event, and it doesn't seem to have any effect ?

>> CaFree works, so that is not the source of the problem.

><snip>

>I also confirmed to myself that setting Action to caFree on Modal forms
>does appear to have a leak.  Since I confirmed this in Delphi 1, the only
>time I have used caFree was with modeless MDI children...

Well, I have the same problem with modeless MDI children too ...
But as long as I remember to remove every TWinControl in the childs OnDestroy
event, there is no leak :-)

--
|| Bjorn K. Nilssen      // http://home.sol.no/~bjoernk/  // mainly old 3D ||

Re:Q: Action := caFree


In article <34b1b7b8.1617...@news.tcp.co.uk>, s...@tcp.co.uk (Simon R Knight)
wrote:

[SNIP]

Hi Simon.

Correct me if I'm wrong here chaps and chapesses. But isn't the "preferred"
way of freeing up modal forms as follows?

        MYform := TMYform.Create(whatever);
        try
          MYForm.ShowModal;
        finally
          MYForm.Free;
          MYForm := Nil;
        end;

I was under the impression that Action := caFree is best used for non-modal
Forms....ie Action := caFree in FormClose event and MYFORM := Nil in
FormDestroy event.

Happy New Year.

Naz.

-------------------------------------------------------
Please remove xy from azizanxy to reply. Sorry
-------------------------------------------------------
The spam harvesters will have a ball with this puppy:
classo...@psbgm.qc.ca
root@localhost
admin@localhost
r...@mailloop.com
jo...@mailloop.com
jor...@earthlink.net
Chairman Reed Hundt: rhu...@fcc.gov
Commissioner James Quello: jque...@fcc.gov
Commissioner Susan Ness: sn...@fcc.gov
Commissioner Rachelle Chong: rch...@fcc.gov
abuse@localhost
postmaster@localhost admin@localhost
hostmas...@BEST.COM
ant...@PACIFIC-TECH.COM

Re:Q: Action := caFree


On Tue, 06 Jan 1998 06:55:19 GMT, aziza...@gto.net.om (Nazar Aziz)
wrote:

Quote
>In article <34b1b7b8.1617...@news.tcp.co.uk>, s...@tcp.co.uk (Simon R Knight)
>wrote:

>[SNIP]

>Hi Simon.

>Correct me if I'm wrong here chaps and chapesses. But isn't the "preferred"
>way of freeing up modal forms as follows?

>        MYform := TMYform.Create(whatever);
>        try
>          MYForm.ShowModal;
>        finally
>          MYForm.Free;
>          MYForm := Nil;
>        end;

Preferred by who ?      : )

Unfortunately (or fortunately?) I have no knowledge of VB, C or C++,
as Delphi is my first programming language, so most of the code that I
use is from the Delphi/Win API helpfiles, and my growing collection of
Delphi books. My normal way of *freeing* (as distinct from closing) a
form would be to use the following line of code in a forms OnClose
event:

  Action := caFree;

I own both Delphi 1 and Delphi 3, and their helpfiles don't indicate
that anything further is necessary. MDI Child forms and modal forms
are not described as incompatible with the above line of code, quite
the reverse. There is every reason to infer from the helpfiles, that
this line of code should work for all forms. The default TCloseAction
of an MDI Child form versus a modal form are mentioned, but of course
that is without significance here.

Even my "Mastering Delphi 3" book by "Marco Cantu" does not indicate
that a bug exists with the above line of code, and presents the
TCloseAction options, similarly to the helpfile.

To ensure that resources are properly freed when a form closes, I have
started using the following lines of code with forms that are rarely
if ever required (ie. A registration form) ...

  try
    Application.CreateForm(TForm2, Form2);
    Form2.ShowModal;
  finally
    Form2.Free;
  end;

In your example above, your give ...

Quote
>        MYform := TMYform.Create(whatever);
>        try
>          MYForm.ShowModal;
>        finally
>          MYForm.Free;
>          MYForm := Nil;
>        end;

I have never used "MYForm := Nil" before, or have ever considered it,
as the helpfile has never suggested that this is necessary ?  I am and
have been under the impression that calling something like
"MYForm.Free" was all that is needed to destroy a form ?

Quote
>I was under the impression that Action := caFree is best used for non-modal
>Forms....ie Action := caFree in FormClose event ...

Someone wrote to me the other day, and said that Borland have
recommended that caFree is not used ?  If this is correct, then they
really should have modified their helpfiles when they rewrote them for

Delphi 3, if not before ?

Quote
>and MYFORM := Nil in
>FormDestroy event.

Shouldn't the pointer be set to nil by the destroy procedure called by
freeing the form ?

In another article here someone writes that they are having to
explicitly free controls before destroying a form, and this sounds
even more extreme ! Is it the case that under certain circumstances
calling the "Free" procedure for a form doesn't actually release the
memory or resources for the controls that are owned by that form ?

Up to now, I have only made a few experiments using the Windows for
Workgroups 3.11 Program Manager's "About Program Manager" dialog box
for reporting percentage of available system resources, so I haven't
checked what might be going on with different kinds of forms. I have
now downloaded a nice project from the "Delphi Developers Journal",
that will give me more info, and when I get the time to build it, then
I will test everything in sight !

Quote

>Happy New Year.

I sure hope so !      : )

Simon R Knight

Re:Q: Action := caFree


In article <34b28a96.1758...@news.tcp.co.uk> s...@tcp.co.uk (Simon R Knight) writes:

Quote
> I am and
>have been under the impression that calling something like
>"MYForm.Free" was all that is needed to destroy a form ?

Without diving into the rest of your message, Simon, a safer way than free is
"MyForm.Release."  As you can see from the implementation in FORMS.PAS, this
routine posts a message to the form's message-queue that causes the form to be
freed when the message is dequeued -- reasonably assuring that any pending
messages in the queue that may also refer to the form have been processed
before the form actually ceases to exist.

Re:Q: Action := caFree


I use caFree in the OnClose event but have not looked at the resources to see
if there is a problem.

The mention of the line MyForm := nil is interesting.  I use that line, not in
Form.Destroy but also in the OnClose event, when I want to be able to test
whether the form exists.  For example, FormA is the main form and FormB is a
secondary form, nonmodal.  A button or menu event on FormA will launch FormB.
Or, if the FormB already exists then then it just shows FormB (brings to top
and sets focus to FormB).  In the FormB onClose:
action := caFree;
FormB := nil;

In the click routine for the button that launches FormB:
if not assigned(FormB) then application.CreateForm(TFormB, FormB);
FormB.show;

Hope this helps, but admittedly no comment on the resource issue.
/js

Re:Q: Action := caFree


On 6 Jan 1998 19:50:01 -0700, news-re...@sundialservices.com (Sundial

Quote
Services) wrote:
>In article <34b28a96.1758...@news.tcp.co.uk> s...@tcp.co.uk (Simon R Knight) writes:
>> I am and
>>have been under the impression that calling something like
>>"MYForm.Free" was all that is needed to destroy a form ?

>Without diving into the rest of your message, Simon, a safer way than free is
>"MyForm.Release."  As you can see from the implementation in FORMS.PAS, this
>routine posts a message to the form's message-queue that causes the form to be
>freed when the message is dequeued -- reasonably assuring that any pending
>messages in the queue that may also refer to the form have been processed
>before the form actually ceases to exist.

That leaves me wondering how I might ensure that the message is
processed before the form ceases to exist. At the moment I am using
the versions of Delphi that do not come with source code, so it isn't
possible to see how Borland has implemented many things. All I have is
the helpfile which - a little unhelpfully - says that "Release is
obsolete for Delphi applications".

The best approach for me - it would seem - will be to keep all the
suggested methods in mind, and try them one by one until something
works in any particular situation; begining with the most conventional
methods first.

Thanks!

Simon R Knight

Re:Q: Action := caFree


Release is obsolete???  WHERE DOES IT SAY THAT!?!??!?!?!?!

Shane Bridges
kinet...@ix.netcom.com

Quote
Simon R Knight wrote:
> On 6 Jan 1998 19:50:01 -0700, news-re...@sundialservices.com (Sundial
> Services) wrote:

> >In article <34b28a96.1758...@news.tcp.co.uk> s...@tcp.co.uk (Simon R Knight) writes:
> >> I am and
> >>have been under the impression that calling something like
> >>"MYForm.Free" was all that is needed to destroy a form ?

> >Without diving into the rest of your message, Simon, a safer way than free is
> >"MyForm.Release."  As you can see from the implementation in FORMS.PAS, this
> >routine posts a message to the form's message-queue that causes the form to be
> >freed when the message is dequeued -- reasonably assuring that any pending
> >messages in the queue that may also refer to the form have been processed
> >before the form actually ceases to exist.

> That leaves me wondering how I might ensure that the message is
> processed before the form ceases to exist. At the moment I am using
> the versions of Delphi that do not come with source code, so it isn't
> possible to see how Borland has implemented many things. All I have is
> the helpfile which - a little unhelpfully - says that "Release is
> obsolete for Delphi applications".

> The best approach for me - it would seem - will be to keep all the
> suggested methods in mind, and try them one by one until something
> works in any particular situation; begining with the most conventional
> methods first.

> Thanks!

> Simon R Knight

Go to page: [1] [2]

Other Threads