Solved

Delphi 3 Close vs Free to close a form

Posted on 1998-03-13
5
157 Views
Last Modified: 2010-04-06
I have been told that when closing a form, Close merely hides the form whereas Free not only closes the form but frees up the resources. My problem is this:

I am writing a program to administer a questionnaire which will consist of a large number of forms with each leading to the next or returning to the previous form, or being able to cancel the operation from any form. Example of the code I am using (from Form2) is:

procedure CancelButtonClick(Sender: Tobject);
begin
  Close;
end;

procedure NextButtonClick(Sender: Tobject);
begin
  DoSomething;
  Form2.Hide;
  Form3 := TForm3.Create(Self);
  Form3.ShowModal;
  Close;
end;

procedure BackButtonClick(Sender: Tobject);
begin
  DoSomething;
  Form2.Hide;
  Form1 := TForm3.Create(Self);
  Form1.ShowModal;
  Close;
end;

This works great and I am having no problem, but if the close does not free the resources used by the form I will soon run out of them when moving forward and backward through the forms. I have tried using Free instead of close, but this always gives me an access error when canceling from a form.
0
Comment
Question by:DeCordova
  • 2
  • 2
5 Comments
 
LVL 1

Accepted Solution

by:
pjdb earned 100 total points
ID: 1360483
If you create the form each time you call it, for sure you will run out of ressouces. you have to create the form only once in the all program.
on the cancel button, you don't need to free the form, the program will do it at the termination.

JDB
0
 

Author Comment

by:DeCordova
ID: 1360484
If I create the form only once what happens when I close it and then need to return to it by going back in the questionnaire? When I use the Close command, am I freeing up the resources or do I need to use Free to do it? I tried using Free instead of Close but get errors when I try to cancel one of the forms. My main concern is running out of resources because I will be using a very large number of forms.

By the way, I think your answer is a record, I only posted the question a few minutes ago.


0
 
LVL 1

Expert Comment

by:pjdb
ID: 1360485
In fact it denpends on the way you quit a form.
If you close the form, then use the whoe method to make it reappear
If you destroy the form (with the free method) then you need to create it again to be able to show it again.

As far as i know, the close method does not free the ressources. you have to call the free method to do it or it will be done when you quit the application.

The errors you get with the way you use the free method occour because you call the free method from the object itself. i suggest you to use a structure like the following one
var
  actual_form:byte;

sub main;
begin
  actual_form:=1;
  while (actual_form>0) and (actual_form<max_form) do
    case actual_form of
    1:
      begin
        form1:=Tform1.create(self);
        form1.showmodal;
        form1.free;
      end;
    2:
.....
    else
      actual_form:=0;
    end;
end;

procedure form1.backbutton;
begin
  dec(actual_form);
end;

procedure form1.nextbutton;
begin
  inc(actual_form);
end;

procedure form1.cancelbutton;
begin
  actual_form:=0;
end;

0
 

Expert Comment

by:ermac
ID: 1360486
Have you ever thought of using the TNoteBook component??
You can use it to display different questions but still using the same form...

0
 

Author Comment

by:DeCordova
ID: 1360487
That did it! I feel stupid because I have used that while do loop for the same purpose for years and didn't think of using it here. Thanks for your prompt and accurate answer.

0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
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…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

749 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