Access violation when working with multiple forms

Hello,

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!
DastasAsked:
Who is Participating?
 
Kent OlsenData Warehouse Architect / DBACommented:

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,
Kent
0
 
Kent OlsenData Warehouse Architect / DBACommented:
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;
and
  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,
Kent
0
 
DastasAuthor Commented:
Hi,

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?
0
Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

 
Kent OlsenData Warehouse Architect / DBACommented:

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.


Kent


0
 
DastasAuthor Commented:
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.
0
 
Kent OlsenData Warehouse Architect / DBACommented:

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.


Kent
0
 
DastasAuthor Commented:
I have attached the two unit files and the two header files.
tmp.zip
0
 
DastasAuthor Commented:
That did solve it!

Thank you very much for the quick solution!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.