[SOLVED]Variable declared private in class cause crash while declared locally it works
-
Hello,
Well If I run normally with CTRL+R just a windows error that the app has stopped working with not usefull information.
With Debug Mode F5 I got a window saying the application stopped because it received a signal from the operating system, Name of the signal SIGSEGV, signification: Segmentation Fault.
-
Looks like some answers disappeared, did you got the others ?
-
- MainWindow.h
@private:
QTextEdit *m_teLog;@
is like that? and bring SIGSEGV? can you post some more code please. as this is just a way that the OS is managing the memory and I know how painful is to debug because I had it a lot. but is only in the way that you initialize or access a variable that it was not initialised properly or on linux if it wasn't deleted.
so if you can bring some more code from both c++ and h++ file
- MainWindow.h
-
SGaist : I just saw a question about the location of the seg fault, that happens apparently on two possible lines
first line of the mainwindow constructor.
@m_window = new QWidget;@or seting the layout
@m_window->setLayout(layout);@What is making the difference is if either I initialize the m_teLog variable to NULL in my private variables declaration in the header file. I can set all others to NULL or not and it does work till the setLayout, only changing for m_teLog makes the SIGSEGV location change.
Here is the full class header and , nothing really fancy
Header :
@#ifndef MAINWINDOW_H
#define MAINWINDOW_H#include <QMainWindow>
#include <QPushButton>
#include <QTextEdit>
#include <QCoreApplication>
#include <QGridLayout>class MainWindow : public QMainWindow
{
Q_OBJECTpublic:
MainWindow(QWidget *parent = 0);private:
QWidget *m_window= NULL;
QPushButton *m_btnConnect= NULL;
QPushButton *m_btnDisconnect= NULL;
QPushButton *m_btnReset= NULL;
QTextEdit *m_teLog;};
#endif@
Code
@#include "mainwindow.h"MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent)
{
m_window = new QWidget;m_btnConnect = new QPushButton("Connect"); m_btnDisconnect = new QPushButton("Disconnect"); m_btnReset = new QPushButton("Reset"); m_teLog = new QTextEdit; QGridLayout *layout = new QGridLayout; layout->addWidget(m_btnConnect, 0, 0); layout->addWidget(m_btnDisconnect, 0, 1); layout->addWidget(m_btnReset, 0, 2); layout->addWidget(m_teLog, 1, 0, 1, 3); m_window->setLayout(layout); m_window->show();
}@
-
Are you running on windows ?
-
yes it's on windows and with mingw compiler.
-
can you show as how is your main.cpp do you set any layout there?
-
here is the main.cpp
@#include <QApplication>#include "mainwindow.h"
int main(int argc, char *argv[])
{
QApplication app(argc, argv);MainWindow mainWin; return app.exec();
}#include <QApplication>
#include "mainwindow.h"
int main(int argc, char *argv[])
{
QApplication app(argc, argv);MainWindow mainWin; return app.exec();
}@
-
I can not see mainWin.show();
Also is a type error that is doubled or you just have it like that? -
okay, yeah of course it was a mistake.
I've got it working apparently by making two changes.
First I added the mainWin.show(); as you indicated. It was working but I had two windows, an empty one, and the one with my three buttons and TextEdit.
So then I changed in the MainWindow.cpp to remove the m_window->show(); and replaced it by this->setCentralWidget(m_window);
And now everything is looking fine though it don't really explain why it could be working with a local variable before.
-
Out seams like a parental problem of your pointers. This is way it was displaying 2 windows. I'm glad that is working now. Please edit your first post and add [SOLVED] to it
-
yep just did it thanks a lot for the help :)