Access violation when working with multiple forms

Posted on 2008-06-20
Last Modified: 2013-11-17

I'm using C++ Builder. I have two forms, Form1 and Form2. When I click a button in Form1, I make Form2 visible (for this I include Uni2.cpp in Unit1.cpp). Now, when I click a button on Form2, I assign a TEdit control on Form1 a value from a listbox on Form2 (for this I include Uni1.h in Unit2.cpp). This compiles, but it gives an access violation when I click that button in Form2.

So, my question is: how can I make it so I can access Form1 components from Form2?

Thanks in advance!
Question by:Dastas
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
LVL 45

Expert Comment

by:Kent Olsen
ID: 21831768
Hi Dastas,

I don't know which version of C++ Builder you're using, but this applies to most....

The header file for every C++ Builder generated form (File/New Form) contains a declaration of the form as the last non-comment line in the source file.  The main application instantiates an instance of the form and initializes it.  The pointer is referenced via the declaration in the header file.

In your case the two header files probably have lines like this:

  extern PACKAGE TForm1 *Form1;
  extern PACKAGE TForm2 *Form2;

If you're using these variables to access the forms (instead on instantiating new ones) you're 90% of the way home.  The last detail is that to switch between forms, don't close a form.  That will destroy the form and invalidate the pointers.  Use the Hide() or SetFocus() methods to switch between the forms.

Good Luck,

Author Comment

ID: 21832127

I'm using C++ Builder 2007, if that makes any difference.

Yes, my two header files do have those declarations. And I am using those to access the form, at least I think I do? Also, no forms are closed. I use Form2->Visible = 1; to make the second form visible. The second form has an Ok button, with the following OnClick event:

void __fastcall TForm2::OkClick(TObject *Sender)
      Form1->Cale->Text = Dirsel->Directory;

"Cale" is a TEdit control on Form1. This gives me an access violation when I run the program and click this button.

Unit2.cpp includes Unit1.h so I can access Form1, and Unit1.cpp includes Unit2.cpp so I can access Form2->Visible.

Any ideas what I'm missing?
LVL 45

Expert Comment

by:Kent Olsen
ID: 21832228

My guess is that Dirsel->Directory is the bad pointer.  Dirsel is probably a pointer to a modal dialog box that you're using to select a directory or file.  Make sure that the instance still exists.

If you can set a breakpoint on that line, display the value of Dirsel and Dirsel->Directory.


Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!


Author Comment

ID: 21832322
I doubt that's it. Dirsel is a directory listbox and since it's on the same form the button is on, it has to exist.

Besides, if I replace the line with this:

Form1->Cale->Text = "This will crash!";

The access violation still happens, so it has to be with the LHS.
LVL 45

Expert Comment

by:Kent Olsen
ID: 21833040

How long is the source code?  Can you post it?

Also, try doing a "find" on Form1 and Form2.  Make sure that it's not redefined somewhere.


Author Comment

ID: 21833176
I have attached the two unit files and the two header files.
LVL 45

Accepted Solution

Kent Olsen earned 125 total points
ID: 21833356

Ok..  It's starting to take shape.....  It looks like you might have an oddly set up project.

In Unit1.CPP you #include "Unit2.CPP".  That's an odd thing to do.  It results in ALL of the Unit2.cpp source being compiled as part of Unit1.  Note that Unit2.CPP has the line:

  TForm2 *Form2;

It appears that you now have a variable in Form1 called Form2, and a variable in Form2 called Form2.

In the .CPP files, include both of the header (.h) files, but do not include the source (.cpp) files.

I'm betting that that will solve this.  If not, we should at least move the error to something easier to spot (like a module not being part of the project).

Good Luck,

Author Comment

ID: 21833404
That did solve it!

Thank you very much for the quick solution!

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Here is a helpful source code for C++ Builder programmers that allows you to manage and manipulate HTML content from C++ code, while also handling HTML events like onclick, onmouseover, ... Some objects defined and used in this source include: …
Jaspersoft Studio is a plugin for Eclipse that lets you create reports from a datasource.  In this article, we'll go over creating a report from a default template and setting up a datasource that connects to your database.
The viewer will learn how to synchronize PHP projects with a remote server in NetBeans IDE 8.0 for Windows.
The viewer will learn how to use and create new code templates in NetBeans IDE 8.0 for Windows.

734 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