List Info

Thread: Closing and freeing a form from itself




Closing and freeing a form from itself
country flaguser name
United States
1969-12-31 18:00:00

Hello all:

I remember seeing a tip from ... somewhere on this issue. I cannot
seem to dig it up now.

I would like to have a global variable for a form:

frmGlobal: TfrmGlobal;

I would like to instantiate it from any place:

frmGlobal := TfrmGlobal.Create(Application);

and show it as a non-modal form:

frmGlobal.Show;

When I am done with the form, I would like to close it and free it up.
Of course, the problem with this is that, at a later date, I may want
to create it again. To avoid a memory leak, I clearly would not want
to create a new instance if the existing instance still exists. I
could use:

If (Assigned(frmGlobal)) Then ...

except that assigned merely asks the variable, "are you NIL?" Since
merely freeing the variable does not set it to NIL, the system thinks
it is still there.

Since the form could be created anywhere, and the form should be
deleted when it is closed, the best way to deal with it would seem to
be figuring out a way to free the form and set the global variable to
NIL from the OnDestroy event. I have had a number of problems doing
this (chicken or egg problem.) A lot of AV's come out of
experimenting with this.

Does anyone have a good solution to how to free and NIL [the global
variable] form from inside itself?

Thanks
jamie

__._,_.___
.

__,_._,___
Re: Closing and freeing a form from itself
country flaguser name
United States
1969-12-31 18:00:00

Straight out of the Delphi manual (the application
creates a global variable automatically as the form's
name when you create a new form):

Put this in the form's OnClose event:
MyForm.Free;
MyForm := nil;

To create it (from anywhere):

If MyForm <> nil Then
MyForm := TMyForm.Create(mainForm)

(I always make the mainform the owner unless it's got
an obvious child relationship to something else, that
way it always gets freed during a GPF situation)

If you create more than one, you can make sure
you've freed them all by iterating through the
Screen.Forms variable looking for that class type:

For i := 0 to Screen.Forms.count-1 do
If Screen.Forms[i] is TMyform then MyForm.close;

See the demo's example under the folder:
demos/db/MastApp. In fact, that application has an
example of many useful tips to use in your own app.

Dave

--- "Jamie L. Mitchell&quot; < jamiemi%40frontiernet.net">jamiemifrontiernet.net>
wrote:

> Hello all:
>;
> I remember seeing a tip from ... somewhere on this
>; issue. I cannot
&gt; seem to dig it up now.
>;
> I would like to have a global variable for a form:
&gt;
> frmGlobal: TfrmGlobal;
>
> I would like to instantiate it from any place:
&gt;
> frmGlobal := TfrmGlobal.Create(Application);
>
> and show it as a non-modal form:
&gt;
> frmGlobal.Show;
>;
> When I am done with the form, I would like to close
&gt; it and free it up.
> Of course, the problem with this is that, at a
> later date, I may want
>; to create it again. To avoid a memory leak, I
> clearly would not want
>; to create a new instance if the existing instance
> still exists. I
> could use:
>;
> If (Assigned(frmGlobal)) Then ...
>
> except that assigned merely asks the variable, "are
> you NIL?" Since
&gt; merely freeing the variable does not set it to NIL,
>; the system thinks
&gt; it is still there.
>
> Since the form could be created anywhere, and the
> form should be
> deleted when it is closed, the best way to deal with
>; it would seem to
> be figuring out a way to free the form and set the
> global variable to
> NIL from the OnDestroy event. I have had a number
&gt; of problems doing
&gt; this (chicken or egg problem.) A lot of AV's come
>; out of
> experimenting with this.
&gt;
> Does anyone have a good solution to how to free and
> NIL [the global
&gt; variable] form from inside itself?
>
> Thanks
&gt; jamie
&gt;
>
>

__________________________________________________________
8:00? 8:25? 8:40? Find a flick in no time
with the Yahoo! Search movie showtime shortcut.
http://tools.search.yahoo.com/shortcuts/#news

__._,_.___
.

__,_._,___
Re: Closing and freeing a form from itself
country flaguser name
United States
2007-03-30 10:52:52


Hi Jamie:

I do something like this, but with modal forms.

You can create a class function or a class procedure:

.

.

.

public

{ Public declarations }

class function FunctionName(parameters ):type;

.

.

.

Next in the implementation section ...

*implementation*

{$R *.dfm}

class function TFormName.FunctionName(parameters ...):type;

Var ...

begin

with Create(Application) do

try

... // your sentences here

Showmodal;

...

finally

Free;

end;

You can call this Function from any other form ...

TFormName.FunctionNAme(parameters ...);

Don't forget to put the name of class function or class procedure in the
uses section.

Regards,

Sami,

Jamie L. Mitchell escribió:
>
&gt; Hello all:
>;
> I remember seeing a tip from ... somewhere on this issue. I cannot
&gt; seem to dig it up now.
>;
> I would like to have a global variable for a form:
&gt;
> frmGlobal: TfrmGlobal;
>
> I would like to instantiate it from any place:
&gt;
> frmGlobal := TfrmGlobal.Create(Application);
>
> and show it as a non-modal form:
&gt;
> frmGlobal.Show;
>;
> When I am done with the form, I would like to close it and free it up.
> Of course, the problem with this is that, at a later date, I may want
>; to create it again. To avoid a memory leak, I clearly would not want
>; to create a new instance if the existing instance still exists. I
> could use:
>;
> If (Assigned(frmGlobal)) Then ...
>
> except that assigned merely asks the variable, "are you NIL?" Since
&gt; merely freeing the variable does not set it to NIL, the system thinks
&gt; it is still there.
&gt;
> Since the form could be created anywhere, and the form should be
> deleted when it is closed, the best way to deal with it would seem to
> be figuring out a way to free the form and set the global variable to
> NIL from the OnDestroy event. I have had a number of problems doing
&gt; this (chicken or egg problem.) A lot of AV's come out of
> experimenting with this.
&gt;
> Does anyone have a good solution to how to free and NIL [the global
&gt; variable] form from inside itself?
>
> Thanks
&gt; jamie
&gt;
>

[Non-text portions of this message have been removed]

__._,_.___
.

__,_._,___
Re: Closing and freeing a form from itself
country flaguser name
United States
1969-12-31 18:00:00

The problem you are trying to solve is unnecessary.
It is not good strategy to attempt to use the frmGlobal variable as a
flag as to whether or not the form has been created.
It is simpler and more reliable just to "have a look first"; whether
the form has already been created.

procedure TForm1.ShowGlobalForm;
var
frmGlobal: TfrmGlobal;
begin
frmGlobal := Application.FindComponent('frmGlobal') as TfrmGlobal;
if not Assigned(frmGlobal) then
frmGlobal := TfrmGlobal.Create(Application);
frmGlobal.Show;
end;

Form variables of global scope (such as frmGlobal in your original
post) are not critical to a form's existence. They are just a handy
reference to the form for the use of the programmer.
The "real" reference to the form that matters (ie. the one that lives
and dies with the form) is the one in the Components list of the
owner.
Normally of course the global form variable will contain the same
value (pointer) as the value in the Components list. But even if you
overwrite the global variable with a reference to a new instance, or
accidentally set it to Nil, the owner the form still has the
reference to the original instance and will ensure it (and any other
instances of the form it owns) are finally freed.

--- In delphi-en%40yahoogroups.com">delphi-enyahoogroups.com, "Jamie L. Mitchell&quot; <jamiemi...>
wrote:
>
&gt; Hello all:
>;
> I remember seeing a tip from ... somewhere on this issue. I cannot
&gt; seem to dig it up now.
>;
> I would like to have a global variable for a form:
&gt;
> frmGlobal: TfrmGlobal;
>
> I would like to instantiate it from any place:
&gt;
> frmGlobal := TfrmGlobal.Create(Application);
>
> and show it as a non-modal form:
&gt;
> frmGlobal.Show;
>;
> When I am done with the form, I would like to close it and free it
up.
&gt; Of course, the problem with this is that, at a later date, I may
want
> to create it again. To avoid a memory leak, I clearly would not
want
> to create a new instance if the existing instance still exists. I
> could use:
>;
> If (Assigned(frmGlobal)) Then ...
>
> except that assigned merely asks the variable, "are you NIL?" Since
&gt; merely freeing the variable does not set it to NIL, the system
thinks
> it is still there.
>
> Since the form could be created anywhere, and the form should be
> deleted when it is closed, the best way to deal with it would seem
to
&gt; be figuring out a way to free the form and set the global variable
to
&gt; NIL from the OnDestroy event. I have had a number of problems doing
&gt; this (chicken or egg problem.) A lot of AV's come out of
> experimenting with this.
&gt;
> Does anyone have a good solution to how to free and NIL [the global
&gt; variable] form from inside itself?
>
> Thanks
&gt; jamie
&gt;

__._,_.___
.

__,_._,___
[1-4]

about | contact  Other archives ( Real Estate discussion Medical topics )