Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

ShowModal generates "Access violation" error

971 views
Skip to first unread message

Sotiris Rentoulis

unread,
Jul 6, 2002, 12:03:36 PM7/6/02
to
Dear friends,

I encountered recently a very serious problem: I have a main
form in my project and several other "secondary" forms.

I try to open a secondary form from the main form, using
the (well-known) line of code: "Form3->ShowModal();", which
is called by a menu-item event handler.

Then -here is the problem- I get this message after execution of this line:
"Project ... raised exception class EAccessViolation with message
'Access violation at address ... in module ....exe. Read of address
FFFFFFFF' ...".

The most curious thing is that the 2nd or 3rd or ... (place here a small
random number) time
I try to open the form (always via the menu) I succeed!!! And I haven't
encountered this problem
no other time until now with the same or other project!!!

I tried almost everything (re-build, changing project options, making of a
new project, etc)
and I can't find a solution.

Could anybody help?

Thanks in advance, Sotiris Rentoulis.

PS: I use C++ Builder 6.0 Pro and Windows 98.

liz albin

unread,
Jul 6, 2002, 12:23:02 PM7/6/02
to
Sotiris Rentoulis said,

>
> I tried almost everything (re-build, changing project options, making of a
> new project, etc)
> and I can't find a solution.

Access violations are almost always the result of ... well... a misuse
of memory on the part of the appliction program. Often the line at
which the program throws an exception is not the source of the problem.

Have you created a tiny program which demonstrates your AV? If so,
post it and someone will try to help.

Have you gone through all of your pointers and made sure that you
allocate memory for them? The code below for example will cause
problems because memory for Something hasn't been allocated:

TSomeVCLType * Something;
TSomeVCLType * SomethingElse = new TSomeVCLType(this);
SomethingElse->Caption = Something->Caption;

Did you delete something already deleted?

SomeType * Something = new SomeType;

// do stuff
delete SomeType;
// do more stuff

delete SomeType;

Did you mismatch the new/delete and new[]/delete[] operators?

SomeType * Something = new SomeType[12];
// do stuff

delete Something;

etc.

good luck
--
liz

Michael Warner

unread,
Jul 6, 2002, 2:42:46 PM7/6/02
to
On Sat, 6 Jul 2002 19:03:36 +0300, "Sotiris Rentoulis"
<rent...@hotmail.com> wrote:

> I encountered recently a very serious problem: I have a main
>form in my project and several other "secondary" forms.
>
> I try to open a secondary form from the main form, using
>the (well-known) line of code: "Form3->ShowModal();", which
>is called by a menu-item event handler.
>
> Then -here is the problem- I get this message after execution of this line:
>"Project ... raised exception class EAccessViolation with message
>'Access violation at address ... in module ....exe. Read of address
>FFFFFFFF' ...".

The problem is almost certainly in the code you've written for the
form's constructor and relevant events - OnCreate, OnActivate, OnShow.
You should be able to pinpoint it by looking at the call stack, or
stepping through that code.

Another possibility is a memory corruption caused earlier which
happens not to bite you until that form is opened.

Remy Lebeau [TeamB]

unread,
Jul 6, 2002, 5:16:54 PM7/6/02
to
Errors at address FFFFFFFF mean that you dereferenced a NULL pointer, most
likely because the object that the pointer you're using hasn't been
allocated yet.


Gambit

"Sotiris Rentoulis" <rent...@hotmail.com> wrote in message
news:3d27148a_2@dnews...

Sotiris Rentoulis

unread,
Jul 7, 2002, 6:16:51 AM7/7/02
to
Liz,

I thnk that my problem is not one of memory misusing. The cause of it is the following:
In my project I use a main form and 20+ secondary forms. The main form has a menu
associated with an action list. Each time you select a menu item a secondary form is
opened, via a SecondaryForm->ShowModal() command. All forms are auto-created,
so the C++ Builder creates a list of commands like:

Application->CreateForm(__classid(TFormMain), &FormMain);
Application->CreateForm(__classid(TFormAbout), &FormAbout);
etc...

in project .cpp file. And, of course, it adds a line like the following on the end:

Application->Run();

When my programs run, when I select a menu item IMMEDIATELY the
secondary form has not created yet (I know that, because I have defined
breakpoints in Application->CreateForm lines), so the pointer to it is not
defined. But if I wait for some time and then select the menu item, the
program works fine. [Thanks to Micha Gerstenberg for his help!!!]

The problem is that I thought that any application starts to run AFTER
the "Application->Run" command and not prior this. Am I wrong or
right?

Thanks again, Sotiris.

liz albin

unread,
Jul 7, 2002, 10:10:09 AM7/7/02
to
Sotiris Rentoulis said,

> I thnk that my problem is not one of memory misusing.

But you /are/ misusing memory (that's what the AV indicates). What's
probably happening is that you're referring to something /before/ it's
fully created.

> The problem is that I thought that any application starts to run AFTER
> the "Application->Run" command and not prior this. Am I wrong or

If you're using the OnCreate event then you won't know which forms have
actually been created. So don't use it, it doesn't work consistently
in BCB. Move everything to your forms' constructors.
--
liz

Sotiris Rentoulis

unread,
Jul 7, 2002, 4:17:49 PM7/7/02
to
liz albin said:
> Sotiris Rentoulis said,
> > I think that my problem is not one of memory misusing.

>
> But you /are/ misusing memory (that's what the AV indicates). What's
> probably happening is that you're referring to something /before/ it's
> fully created.

YES, OF COURSE. The problem is that my Main Form runs BEFORE:
"Application->CreateForm(__classid(TFormSecondary), &FormSecondary);"
although the "Application->Run();" runs AFTER this.

>
> > The problem is that I thought that any application starts to run AFTER
> > the "Application->Run" command and not prior this. Am I wrong or
>
> If you're using the OnCreate event then you won't know which forms have
> actually been created. So don't use it, it doesn't work consistently
> in BCB. Move everything to your forms' constructors.

I did it, but the problem remains exactly the same.

So the question changes form: Is normal for a C++ Builder's main form to
be shown and react to user actions before the execution of "Application->Run()" command?

Thanks one more time, Sotiris.

Remy Lebeau [TeamB]

unread,
Jul 7, 2002, 4:47:41 PM7/7/02
to
That's not the real problem. The problem is that you're placing your access
code in the creation process of the main form. The application itself is
not actually running yet, not until Application->Run() is called. But the
form still has to be created first, and you're calling your code while the
main form is still being created, before the secondary form has even begun
being created yet. That's why you're getting the error, because the
secondary form doesn't exist yet.


Gambit

"Sotiris Rentoulis" <rent...@hotmail.com> wrote in message

news:3d28a3d5_1@dnews...

liz albin

unread,
Jul 7, 2002, 6:32:51 PM7/7/02
to
Sotiris Rentoulis said,

> YES, OF COURSE. The problem is that my Main Form runs BEFORE:
> "Application->CreateForm(__classid(TFormSecondary), &FormSecondary);"

You can't use a variable -- including the global variable -- before
it's been created.

Do yourself a favor, either allow MainForm to have its own instance of
FormSecondary, created in the MainForm constructor, or at least avoid
referencing the global variable until after /all/ forms have been
created.
--
liz

Sotiris Rentoulis

unread,
Jul 8, 2002, 1:22:43 AM7/8/02
to
Dear friends,

Thanks for all your answers, but I think that I found the cause of
this nasty problem. The reason is that I have defined the Visible property
of MainForm as true...

(> That's not the real problem. The problem is that you're placing your


access
> code in the creation process of the main form.

I didn't do this! :-)

> The application itself is
> not actually running yet, not until Application->Run() is called. But the
> form still has to be created first, and you're calling your code while the
> main form is still being created, before the secondary form has even begun
> being created yet. That's why you're getting the error, because the
> secondary form doesn't exist yet.

... so MainForm was shown BEFORE the creation of other forms and
ShowModal caused an Access Violation when it was called too "early".

Thanks again, Sotiris.

Sotiris Rentoulis

unread,
Jul 8, 2002, 1:46:33 AM7/8/02
to
Sorry for my bad English!


0 new messages