Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Trying to close a form, EAccessViolation

Status
Not open for further replies.

BobbaFet

Programmer
Feb 25, 2001
903
NL
This is what my project looks like:

I have 4 forms. The first form is the form containing some
procedures that all other forms use. This form is set as
the projects main form so I can close the others without the
application shutting down. Application.ShowMainForm is set
to false so users cannot accidentally close it. This is the
only autocreated form.

The second form is a splash screen. It contains two labels
a timer, a panel, and a image. This form only exists for
3 seconds max and then is freed by the next form. It also
creates the next form.

The third form has the first user interface. This is what my
users will mostly be using. It contains allot of components.
209 to be exact. It has a global variable called CloseForm
which is boolean. This variable is set to false when the
form is created and in the OnCloseQuery event it is set to
true. Also all the timers are disabled and freed in this
event. This variable is used to break all for and while
loops so that none of them are running while the
is closing. It is just a precaution. In the onClose event
about 20 settings for my application are written to a
IniFile and is also of course freed.

The forth form is like the first form, containing a user
interface and lots of components. 169. I cant have this form
and the previously mentioned form running at the same time
because together they contain to many components and cause
a massive drain on User and System memory.

Now what I have done is this. In the mainform, the one I
mentioned first, I have a procedure like this:

procedure FreeAndLoadAForm(FreeForm: Integer; LoadForm: Integer);
var MyRFormArray: array[1..3] of TForm;
var MyTFormArray: array[1..3] of TFormClass;
begin
MyRFormArray[1] := Form2;
MyRFormArray[2] := Form3;
MyRFormArray[3] := Form4;

MyTFormArray[1] := TForm2;
MyTFormArray[2] := TForm3;
MyTFormArray[3] := TForm4;

MyRFormArray[FreeForm].Free;
Application.CreateForm(MyTFormArray[LoadForm],MyRFormArray[LoadForm]);
end;


This procedure works well in other applications of mine but
in this one I keep getting EAccessViolations when I try to
close the third form. I have get it when I just use Self.Close
(or Free or Release for that matter) Do you know
what could be causing this ??? Everything goes fine until
it actually closes the form, it even does write the inifile
away properly and it also free's it properly. I dont know
what could be causing this. Do you guys have any idea's on
what I could look at to find the solution ???

Thanx allot, [bobafett] BobbaFet [bobafett]

Everyone has a right to my opinion.
E-mail me at caswegkamp@hotmail.com
 
Perhaps one of the other forms are using variables (global?) found on Form 3...
 
No, because the projects mainform is constructed for that.
The mainform contains all var's and proc that can be used
by all. [bobafett] BobbaFet [bobafett]

Everyone has a right to my opinion.
E-mail me at caswegkamp@hotmail.com
 
Can you guys please help me,

I still havent figured this out,
it's really driving me nuts !!!

Thanx in advance, [bobafett] BobbaFet [bobafett]

Everyone has a right to my opinion.
E-mail me at caswegkamp@hotmail.com
 
Have you checked that the FreeForm index is right in all cases and that the indexed form it's trying to free is actually created?

Did you know that you can set a 'FreeOnClose := true' in the OnClose event of a form? I think thats the right name of the property. So you wouldn't need to free it yourself as it would be handled as soon as the form was closed and if you make each form Modal, should get round the problem.

hope that helps
lou
 
Thanx for your reply weez,

I'm going to check that out !!! [bobafett] BobbaFet [bobafett]

Everyone has a right to my opinion.
E-mail me at caswegkamp@hotmail.com
 
Bobba, it's caFree, like this (from the Delphi help)

procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
if MessageDlg('Close application ?', mtConfirmation,
[mbYes, mbNo], 0) = mrYes then
Action := caFree
else
Action := caNone;
end;
 
I dont actually want to close the application, it's to throw
the form out of the memory to be a bit more relaxed with
memory usage. Most computers dont like it when there are 400
components active in the memory. But I take that since we
are not talking about the project's mainform here there is
no danger of the app shutting down.

Thanx, [bobafett] BobbaFet [bobafett]

Everyone has a right to my opinion.
E-mail me at caswegkamp@hotmail.com
 
hi Bobba

Sorry, I just did a direct copy and paste from the D.help. It was just to show you how to use the caFree bit, that's all. Ignore the rest of the eg.

lou
 
I figured that weez I'm now going to try to get it to work
because I was stuck at work this morning. I'll let you know
how it went.

Thank you very much for your help, [bobafett] BobbaFet [bobafett]

Everyone has a right to my opinion.
E-mail me at caswegkamp@hotmail.com
 
Hi weez,

Thanx again for the help, I got it to work partially now,
I can close the mainform (hooray !!!), but I cant recreate
it without errors, but I'm sure that I can get it to
work !!! Otherwise I'll just post the whole thing here ;-)

Thanx again for the help, [bobafett] BobbaFet [bobafett]

Everyone has a right to my opinion.
E-mail me at caswegkamp@hotmail.com
 
We have a program that opens and closes lots of dialogs, and we always use "self.release" in the formclose and "self:=nil" in the formdestroy. Then whenever we want to open the form we run:

if frm = nil then
frm := tfrm.create(nil)
frm.show;

Sometimes (especially when running from the IDE) delphi is strange about freeing memory from forms.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top