tags:

views:

760

answers:

5
+6  Q: 

Form.Release + NIL

Hello,

if Form.Release is called after using the form, it will free all related memory but not set the form variable to nil.

if not assigned (Form1) then
  begin
    Application.CreateForm(Tform1, Form1);
    try
      // Do something
    finally
      Form1.Release
    end;
  end;

To be able to call the same code again, Form1 would have to be set to nil at some point. From the description of Release I cannot do

Form1 := nil;

right after Release, because the Release procedure will return directly after being called and before the form is actually freed. I cannot detect when Form.Release is finished to set the form var to nil.

What is the best way to do this?

Holger

A: 

In Delphi Win32, the appropriate way to free objects is to call

FreeAndNil(Form1)

This does both jobs in a single call.

However, I have a sneaking feeling there's more to your question than meets the eye. Are you using Delphi for .NET - and if so, which version?

Roddy
Except that it does not apply to Forms where you don't use Free but Release...
Roddy, I am using Delphi Win32 (sorry for not mentioning). As far as I understood, for a form Release does a lot more tasks than just a plain FreeAndNil, which would not free components owned by the form. Is this correct?
Holgerwa
Yes, Release does more, but only to defer the calling of Free. You don't need to call Free deferred, so you don't need to call Release and can call Free. Also see http://stackoverflow.com/questions/274523/formrelease-nil#274734
Lars Truijens
+11  A: 

Put the line

  Form1 := nil;

just after the call to Release.

Release is just posting a CM_RELEASE message to the Form which allows the Form to finish what's in its queue (event handlers) before handling the CM_RELEASE message which means normally just calling Free.
So, after calling Release, you should not assume that the Form variable still points to a valid Form, thus putting nil into the variable.

+11  A: 

Release is just a (potentially) deferred Free. The 1st thing you should do after calling Release is nilling the variable.
Then, you'll be safe even if some code tries to reference Form1 before it is actually destroyed. In a case like in your code, it would just safely recreate another Form1 for the new usage without bothering the one being destroyed.

François
+2  A: 

As is mentioned Release is only a deferred Free for a form to use if it wants to Close/Free itself. Other then being deferred it does nothing different from Release. So there is no use in calling Release in that example. Calling Free seems to be more logical. And you can set it to nil after calling Free or use FreeAndNil.

If you still want to use Release that is fine. Just setting the variable value to nil works. Doing that does not make the forum behave differently. But remember that in this case it is more efficient and more deterministic to call Free instead of Release. My preference is to only use Release where is really needed.

Lars Truijens
+3  A: 

Of you could just always call this:

procedure FreeOrReleaseAndNil(var Obj);
var
  Temp: TObject;
begin
  Temp := TObject(Obj);
  Pointer(Obj) := nil;
  if Temp is TCustomForm then
    TCustomForm(Temp).Release
  else
    Temp.Free;
end;

Be sure to check the type after casting to a TObject as you can't test the type of Obj. This should be safe since like Free, Release is non-virtual.

Jim McKeeth
Jim, you can avoid the test on Assigned, as (Temp is TAnyType) would always return False when Temp is nil.
François
Fixed it. Thanks.
Jim McKeeth