Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Delphi 3 Close vs Free to close a form

Posted on 1998-03-13
5
Medium Priority
?
182 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 400 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

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

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…
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 video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…
Suggested Courses

886 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