Link to home
Start Free TrialLog in
Avatar of Dastas
Dastas

asked on

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!
Avatar of Kent Olsen
Kent Olsen
Flag of United States of America image

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
Avatar of Dastas
Dastas

ASKER

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?

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


Avatar of Dastas

ASKER

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.

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
Avatar of Dastas

ASKER

I have attached the two unit files and the two header files.
tmp.zip
ASKER CERTIFIED SOLUTION
Avatar of Kent Olsen
Kent Olsen
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Dastas

ASKER

That did solve it!

Thank you very much for the quick solution!