Board index » delphi » Having trouble guaranteeing the destruction of an image/form

Having trouble guaranteeing the destruction of an image/form

I have a simple form with a TImage on it. I may open
4 or 5 instances of these at once.

If I call the myForm.Release method for each instance
(screen.forms[i].release) this closes the form.

however I am experiencing 2 very odd behaviours -

1. If I then cycle through the open forms I can sometimes(!)
also find the ones that were supposed to have been released.

2. under some circumstances an immediate re-opening of an
image after it has been closed will force the width of the newly
opened image to match the one already closed overriding any settings
I may make - immediately form.release(ing) it again and re-opening
makes it open correctly.

I suspect I may not be destroying the windows and their handles totally.

Is there a better way to destroy a window and its handle than the release
method or can anyone suggest what else I may be doing wrong please ?

-clive-

 

Re:Having trouble guaranteeing the destruction of an image/form


Hi Clive,
    Try <YourForm>.Free or <YourForm>.FreeAndNil

Hope this helps,

Regards,
Ananth B.

Quote
"clive" <clive.wilk...@lodal.net.com> wrote in message

news:MPG.18ebe97c8f77809a989682@newsgroups.borland.com...
Quote

> I have a simple form with a TImage on it. I may open
> 4 or 5 instances of these at once.

> If I call the myForm.Release method for each instance
> (screen.forms[i].release) this closes the form.

> however I am experiencing 2 very odd behaviours -

> 1. If I then cycle through the open forms I can sometimes(!)
> also find the ones that were supposed to have been released.

> 2. under some circumstances an immediate re-opening of an
> image after it has been closed will force the width of the newly
> opened image to match the one already closed overriding any settings
> I may make - immediately form.release(ing) it again and re-opening
> makes it open correctly.

> I suspect I may not be destroying the windows and their handles totally.

> Is there a better way to destroy a window and its handle than the release
> method or can anyone suggest what else I may be doing wrong please ?

> -clive-

Re:Having trouble guaranteeing the destruction of an image/form


Clive,

Quote
"clive" <clive.wilk...@lodal.net.com> wrote in message

news:MPG.18ebe97c8f77809a989682@newsgroups.borland.com...

Quote
> I have a simple form with a TImage on it. I may open
> 4 or 5 instances of these at once.

> If I call the myForm.Release method for each instance
> (screen.forms[i].release) this closes the form.

> however I am experiencing 2 very odd behaviours -

> 1. If I then cycle through the open forms I can sometimes(!)
> also find the ones that were supposed to have been released.

> 2. under some circumstances an immediate re-opening of an
> image after it has been closed will force the width of the newly
> opened image to match the one already closed overriding any settings
> I may make - immediately form.release(ing) it again and re-opening
> makes it open correctly.

> I suspect I may not be destroying the windows and their handles
totally.

> Is there a better way to destroy a window and its handle than the
release
> method or can anyone suggest what else I may be doing wrong please ?

> -clive-

Yep. <g> First, don't use the Form.Release method. It's designed only
for use within one of the Form's own methods; it allows the Form to
clear the message loop before actually being closed. Second, just close
the form:

for i := 0 to Screen.FormCount - 1 do
  Screen.Forms[i].Free;

If this doesn't work, then change the above to:

  FreeAndNil(Screen.Forms[i]).

Ken
---
Ken White
kwh...@adpsi.com

Clipper Functions for Delphi and C++ Builder
http://www.adpsi.com

Re:Having trouble guaranteeing the destruction of an image/form


Quote
> for i := 0 to Screen.FormCount - 1 do
>  Screen.Forms[i].Free;

thanks Ken - and Ananth...

I'll give this a go.

-clive-

Re:Having trouble guaranteeing the destruction of an image/form


Quote
> for i := 0 to Screen.FormCount - 1 do
>   Screen.Forms[i].Free;

ok well - that doesnt work ....

The reason it doesnt is that the form with the image on it
is responding to a mouseUp event which calls a procedure in
another form which eventaually closes the first form

in form1:

begin
 onMouseUp form2.closeroutine(form1.handle);
end;

in Form2;
closeRoutine(etc.)
   form1.free;

The problem is clear - the .Free procedure results
in a return to the "end;" of the mouseUp block which
no longer exists so an exception is raised.

however if I use release it works - but doesn't clear the form from
memory as free does.

Ok so that (after a good deal of experimenting) is my explanation
of events -

It looks like a problem I'm going to have to bodge to make work
unless someone can see another way ?

-clive-

Re:Having trouble guaranteeing the destruction of an image/form


Quote
> however if I use release it works - but doesn't clear the form from
> memory as free does.

> Ok so that (after a good deal of experimenting) is my explanation
> of events -

> It looks like a problem I'm going to have to bodge to make work
> unless someone can see another way ?

Have you tried putting some handler code in the "OnClose" event for the
form?

You need to do these things:

Set an internal flag on the form so the form knows that it needs to free
itself. ie, CloseAction:TCloseAction - set it to "caHide" as default if the
form is an SDI form, or caMinimise if it's an MDI form (see the help for
TCustomForm.OnClose for more details). This flag controls what happens to
the form when it's closed.

In the OnClose event of the form set the "Action" value to the value of the
variable CloseAction.

HTH

Nick

Re:Having trouble guaranteeing the destruction of an image/form


Hmmm... missed the following lines out of the explantion :

Before calling the .release method, set the form's variable "CloseAction" to
"caFree". Then call release as normal.

Other Threads