Solved

URGENT!!! Problem closing modal windows

Posted on 1999-01-26
17
263 Views
Last Modified: 2010-04-06
When I create and show a windows with the command :
 ModalForm:=ModalForm.Create(self);
 ModalForm.ShowModal;

And when I close it with the command close (with the line "action=cafree" on the OnClose event), it makes (Not every time but almost) an error like "error abstract" or "access violation" . When this error happens, there isn't any line in my program that seem to be implicated (shown in blue).
What Can I do???

 
0
Comment
Question by:logosapience
  • 4
  • 3
  • 2
  • +8
17 Comments
 
LVL 5

Expert Comment

by:inter
ID: 1363385
Hi there,
I suppose the parent of the form as you desinated with Self here tries to dispose it too(may be!). Would you please test the following scema: (And also comment out the caFree in your modal forms OnClose:

  ModalForm:=ModalForm.Create(self);
  try
    ModalForm.ShowModal;
    ModalForm.Free;
  except
    ShowMessage('Exception Occurs');
  end;

Your problem appears to me as trying to free and invalid object..
regards, igor
0
 
LVL 1

Author Comment

by:logosapience
ID: 1363386
I tried the try...except method but it didn't seem to catch the exception
I close the modal window within itself, (with the close method on an OK button), if it helps...
0
 
LVL 5

Expert Comment

by:inter
ID: 1363387
Lets do it step by step:

1 - Comment out your close methods and caFree etc.
2 - On you modal forms OK button set ModalResult property to mrOk
3 - Create and show the form as in my previous comment and test

we can make it out do not worry!
regards, igor
0
 
LVL 1

Expert Comment

by:adeng
ID: 1363388
Hi,

I Always use this procedure for modal window :

frmTest := TfrmTest.Create(application);

try
   frmTest.ShowModal;
finally
   frmTest.Free;
   frmTest := nil;
end;

And i'think you can close modal window safely from it self, like : btn1 Click --> ModalResult := mrOK ....

Hope this usefull.

Adeng
0
 
LVL 12

Expert Comment

by:rwilson032697
ID: 1363389
adeng: You should never call frmTest.free, instead call frmTest.Release. This is because there may be pending messages for the form when you destroy it...

logosapience: The line

ModalForm:=ModalForm.Create(self);

should be written like this:

ModalForm:=TModalForm.Create(Application);

Cheers,

Raymond.
0
 
LVL 10

Expert Comment

by:viktornet
ID: 1363390
another example...

with TModeForm.Create(self) do
try
  ShowModal;
  //somestuff...
finally
  Free;
end;
0
 
LVL 13

Expert Comment

by:Epsylon
ID: 1363391
To close a modal form, set its ModalResult property to a nonzero value!
0
 
LVL 10

Expert Comment

by:viktornet
ID: 1363392
Epsylon, adeng has already said that...
0
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
LVL 1

Author Comment

by:logosapience
ID: 1363393
Now, I create my modal windows as the following :
"ModalWindow:=TModalWindow.create(application);"
I show it with :
 try
  ModalWindow.showmodal;
 finally
  ModalWindow.release
 end;
And to close it, I made a button with a modalresult set to mrok.

--> There is still an access violation error.
What could this be?
Note : I create this modal window in a mdi child window. Is there a problem with that?

0
 
LVL 3

Expert Comment

by:Ronald Buster
ID: 1363394
Hi,

You create a modal window in a mdi child window and have problems why I do not know
but in applications I developed I use the following statement to use a modal window :


var MdlW : TModalWindow;

MdlW := TModalWindow.Create(Self)
with MdlW do
  begin
     if ShowModal = mrOk then
       begin
          // do all the things you want to when user hits eg. ok button
       end;
     Free;
  end;

But in the modalwindow form you must not and I repeat must not use a caFree on the OnClose event of the modalwindow form, only use this on the mdi child form.

But the however if the access violation error still occurs try to debug it set a breakpoint and step into your modalwindow form routines and my guess is that it only happens when you close it so set also a breakpoint in the closeroutine. Maybe your are doing something with variables that cause the error.

regards,

Cono


0
 
LVL 1

Author Comment

by:logosapience
ID: 1363395
Thank you cono but the problem is that I want my form to be modal not to see if the user answer ok or not but only for its property to be on top and don't allow the user to click outside to hide it.
I tried to add a button in this window with modalresult:=true on its onclick event which I push when I want to close the window but the problem's still here.

In fact, if anyone knows how to get the same interesting properties (not hiding window before closing it, on topn not clicking outside), it would resolve my problem!
0
 
LVL 20

Expert Comment

by:Madshi
ID: 1363396
Just set the enable property of the other forms to false. Then you'll have the same effect as you've with ShowModal. But I think you should better look for the real problem.
How about putting a breakpoint on the formClose event handler and then go through your sources step by step.
Another thing that you could try:

If you're using this (like you posted in one of your last comments):

ModalWindow:=TModalWindow.create(application);"
I show it with :
 try
  ModalWindow.showmodal;
 finally
  ModalWindow.release
 end

You could (should?) remove the line "action:=caFree", because the release command frees the form later. So you don't need the line "action:=caFree".

Regards, Madshi.
0
 
LVL 1

Author Comment

by:logosapience
ID: 1363397
I already tried the step by step mode but the error not happens everytime so it can be very long and when it happens, it's not due to my variables because there is no internal exception in my program.
0
 

Accepted Solution

by:
garizpe earned 150 total points
ID: 1363398
Could be a diferent answer?

  ModalWindow.showmodal;
    Try
      if  ModalWindow.ModalResult = idYes then
        ... [Your code]
      else
       ...
   Finally
    // if you want catch any error
     Try
        ModalWindow.Free;
     except
       ShowMessage('Error !!!');
     end;
   end;
0
 
LVL 10

Expert Comment

by:viktornet
ID: 1363399
don't use release...

i try this...

with TModaForm.Create(self) do
try
  ShowModal;
  //some other stuff...
finally
  free;
end;
0
 
LVL 1

Expert Comment

by:zwarte
ID: 1363400
I don't quite seem to grasp the problem, but tis is an example of a form with a button that modally generates another and the other is closed with another button.

program Project1;

uses
  Forms,
  Unit1 in 'Unit1.pas' {Form1},
  Unit2 in 'Unit2.pas' {Form2};

{$R *.RES}

begin
  Application.Initialize;
  Application.CreateForm(TForm1, Form1);
  Application.Run;
end.


unit Unit2;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls;

type
  TForm2 = class(TForm)
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form2: TForm2;

implementation

{$R *.DFM}

procedure TForm2.Button1Click(Sender: TObject);
begin
   modalresult:=mrok;
end;

end.

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls;

type
  TForm1 = class(TForm)
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

uses Unit2;

{$R *.DFM}

procedure TForm1.Button1Click(Sender: TObject);
var
   form:Tform2;
begin
   form:=Tform2.create(self);
   form.showmodal;
   form.free;
end;

end.
0
 
LVL 4

Expert Comment

by:jsweby
ID: 5151500
I have had this problem for a long time now and I think I have finally gone some way to solving this.

Add the following line in the FormCreate for the component that calls the Form.Close method (e.g. btnClose):

procedure TForm1.OnCreate(Sender: TObject);
Begin
     btnClose.ControlStyle := btnClose.ControlStyle + [csNoStdEvents];
End;

This stops the compiler trying to action any further events for the control, which is what we need because we are running one of its events but then freeing it in mid-event.
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now