[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2956
  • Last Modified:

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.
1
mathematics
Asked:
mathematics
  • 2
  • 2
1 Solution
 
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
 
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

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now