Solved

Access violation when working with multiple forms

Posted on 2008-06-20
8
861 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
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 

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
 
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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Object Data Source 4 127
Custom ckeditor using builder 10 99
Intellij adding new line in xml 3 71
NOTEPAD++ Meaning of (+) and (-) Icons 6 123
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.

776 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