Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

After TForm1.Action := caFree but Form1 is not nil

Posted on 1999-06-30
4
Medium Priority
?
2,849 Views
1 Endorsement
Last Modified: 2006-11-17
If my codes:
var Form1:TForm1;
procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  Action := caFree;
  Form1 := nil;  // <- Because I have a sentence in
                 // another procedure has the code:
                 // if Form1 = nil then Form1 :=                  //    TForm1.Create(nil);
                 // so I must set Form1 nil.
end;
Question 1:Is that the memory of Form1 is nil? Is that true?
Question 2:I can't repace Form1 := nil with Self := nil;
           why?
Question 3:Do we have another code without using the            name 'Form1' in TForm1.FormaClose and
           set itself with nil?
Give me any suggestion. Thanks.
1
Comment
Question by:mathematics
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 15

Expert Comment

by:simonet
ID: 1385363
Q1)
When you call ACtion := caFree, that means that the memory pointed to Form1 is freed, but that doesn't necessarily make the pointer Form1 nil. Remember that all class variables are pointers, that is, they simply point to addresses in memory where the actual class object is. By calling "Form1 := nil" you simply specify that from now on Form1 doesn't point to a valid location. If you had not called "Form1 := nil", Form1 would still be pointing to a memory location, but that would be a junk address.

Q2) Form1 is a pointer variable, while Self is not a variable, although it looks like it.

Q3) No. Your approach is good. The only thing I'd do different is to call "form1 := nil;" in the OnDestroy event of the form, rather than on the OnClose event.

Yours,

Alex
Athena's Place: http://www.bhnet.com.br/~simonet


0
 
LVL 15

Accepted Solution

by:
simonet earned 200 total points
ID: 1385364
Q1)
When you call ACtion := caFree, that means that the memory pointed to Form1 is freed, but that doesn't necessarily make the pointer Form1 nil. Remember that all class variables are pointers, that is, they simply point to addresses in memory where the actual class object is. By calling "Form1 := nil" you simply specify that from now on Form1 doesn't point to a valid location. If you had not called "Form1 := nil", Form1 would still be pointing to a memory location, but that would be a junk address.

Q2) Form1 is a pointer variable, while Self is not a variable, although it looks like it.

Q3) No. Your approach is good. The only thing I'd do different is to call "form1 := nil;" in the OnDestroy event of the form, rather than on the OnClose event.

Yours,

Alex
Athena's Place: http://www.bhnet.com.br/~simonet 
0
 
LVL 4

Expert Comment

by:Radler
ID: 1385365
Some like this would be better:

TForm? = class(...)
....
public
.....
      destructor Destroy; override;
....
end;

destructor TForm?.Destroy;
begin
      inherited;
      Self:=nil;
end;

works with me.

0
 
LVL 4

Expert Comment

by:Radler
ID: 1385366
Sorry;

I'm very stoned to say this.
My procedure is call a method that close the window and after do the assignment.
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

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 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…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
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

722 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