Link to home
Start Free TrialLog in
Avatar of mathematics
mathematics

asked on

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.
Avatar of simonet
simonet
Flag of Brazil image

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


ASKER CERTIFIED SOLUTION
Avatar of simonet
simonet
Flag of Brazil image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Radler
Radler

Some like this would be better:

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

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

works with me.

Sorry;

I'm very stoned to say this.
My procedure is call a method that close the window and after do the assignment.