Solved

Delphi 3 Close vs Free to close a form

Posted on 1998-03-13
5
152 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

728 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now