• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 309
  • Last Modified:

URGENT!!! Problem closing modal windows

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
logosapience
Asked:
logosapience
  • 4
  • 3
  • 2
  • +8
1 Solution
 
interCommented:
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
 
logosapienceAuthor Commented:
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
 
interCommented:
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
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
adengCommented:
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
 
rwilson032697Commented:
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
 
viktornetCommented:
another example...

with TModeForm.Create(self) do
try
  ShowModal;
  //somestuff...
finally
  Free;
end;
0
 
EpsylonCommented:
To close a modal form, set its ModalResult property to a nonzero value!
0
 
viktornetCommented:
Epsylon, adeng has already said that...
0
 
logosapienceAuthor Commented:
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
 
Ronald BusterOwnerCommented:
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
 
logosapienceAuthor Commented:
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
 
MadshiCommented:
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
 
logosapienceAuthor Commented:
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
 
garizpeCommented:
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
 
viktornetCommented:
don't use release...

i try this...

with TModaForm.Create(self) do
try
  ShowModal;
  //some other stuff...
finally
  free;
end;
0
 
zwarteCommented:
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
 
jswebyCommented:
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

2018 Annual Membership Survey

Here at Experts Exchange, we strive to give members the best experience. Help us improve the site by taking this survey today! (Bonus: Be entered to win a great tech prize for participating!)

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