Solved

Delphi 3 Close vs Free to close a form

Posted on 1998-03-13
5
156 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

808 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