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

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.
mathematicsAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

simonetCommented:
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
simonetCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
RadlerCommented:
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
RadlerCommented:
Sorry;

I'm very stoned to say this.
My procedure is call a method that close the window and after do the assignment.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.