Solved

Access violation when working with multiple forms

Posted on 2008-06-20
8
857 Views
Last Modified: 2013-11-17
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!
0
Comment
Question by:Dastas
  • 4
  • 4
8 Comments
 
LVL 45

Expert Comment

by:Kdo
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;
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
 

Author Comment

by:Dastas
ID: 21832127
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
 
LVL 45

Expert Comment

by:Kdo
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.


Kent


0
 

Author Comment

by:Dastas
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.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 45

Expert Comment

by:Kdo
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.


Kent
0
 

Author Comment

by:Dastas
ID: 21833176
I have attached the two unit files and the two header files.
tmp.zip
0
 
LVL 45

Accepted Solution

by:
Kdo 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,
Kent
0
 

Author Comment

by:Dastas
ID: 21833404
That did solve it!

Thank you very much for the quick solution!
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Update (December 2011): Since this article was published, the things have changed for good for Android native developers. The Sequoyah Project (http://www.eclipse.org/sequoyah/) automates most of the tasks discussed in this article. You can even fin…
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: …
The viewer will learn how to use NetBeans IDE 8.0 for Windows to connect to a MySQL database. Open Services Panel: Create a new connection using New Connection Wizard: Create a test database called eetutorial: Create a new test tabel called ee…
THe viewer will learn how to use NetBeans IDE 8.0 for Windows to perform CRUD operations on a MySql database.

863 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now