Read Access Violation Exception
-
I'm working on a project for a class which involves creating a small "teaching tool." Well, I decided to create a main menu window with pushbuttons that would open another widget containing a picture and information on the topic. I created the first widget and did the slot and everything was wonderful in the world. Now, I tried adding a second pushbutton. This didn't end so well. I created it the exact same way using the exact same commands and pointers and everything, however, it keeps giving me a Read Access Violation Exception. If I comment out the code for the second button, the program works again, even though I running the exact same commands in each method.
It would be appreciated if I could receive help as soon as possible because this project is due friday.
@
void MainMenu::on_PearlHarborButton_clicked()
{
if(!pearlHarbor)
{
pearlHarbor = new PearlHarbor(this);
}
pearlHarbor->show();
pearlHarbor->raise();
pearlHarbor->activateWindow();
}//offending method
void MainMenu::on_WWIIButton_clicked()
{
if(!wwiiDialog)
{
wwiiDialog = new WWIIDialog(this);
}wwiiDialog->show(); wwiiDialog->raise(); wwiiDialog->activateWindow();
}
@Header files:
MainMenu.h
@
#include "ui_mainmenu.h"
#include "pearlharbor.h"
#include "wwiidialog.h"class MainMenu : public QMainWindow, private Ui::MainMenu
{
Q_OBJECTpublic:
explicit MainMenu(QWidget *parent = 0);
private slots:void on_PearlHarborButton_clicked(); void on_WWIIButton_clicked();
private:
void loadImages();
PearlHarbor *pearlHarbor;
WWIIDialog *wwiiDialog;
};
@
PearlHarbor.h
@
class PearlHarbor : public QDialog, private Ui::PearlHarbor
{
Q_OBJECTpublic:
explicit PearlHarbor(QWidget *parent = 0);
private slots:
void on_Close_clicked();
};
@
WWIIDialog.h
@
class WWIIDialog : public QDialog, private Ui::WWIIDialog
{
Q_OBJECTpublic:
explicit WWIIDialog(QWidget *parent = 0);
private slots:
void on_Close_clicked();
};
@
The close clicked method is the same for both, just the respected namespace is different.
@
void on_Close_clicked()
{
class name::hide();
}
@ -
Does your MainMenu constructor initialise pearlHarbor and wwiiDialog to zero? If not they may contain any old rubbish with the result that:
@
if (!wwiiDialog) {
@
may evaluate false even though the value is not a valid pointer. The result is you skip creating the dialog object and try to use the invalid pointer. Using an invalid pointer is a Bad Thing(tm). -
I rearranged the code and initialized pearlHarbor and wwiiDialog in the MainMenu constructor, now it works.