[SOLVED]Variable declared private in class cause crash while declared locally it works
-
Hello.
I'm beginning to check a little on QT for a small project I have and I've an issue which I don't really understand. I'm used to program in C++ and C3, so I thought I would not have a lot of issue using Qt. So it's the first issue and might not be the last. so here it is.
My purpose is to make a simple window so far with 3 buttons on top and a TextArea on bottom, so I've made a class that will be my MainWindow. And so far I've not put any signal yet to manage the button clicks.
Here is the bit of code I have.
@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; //here is the problem 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();
}@
Pretty simple and of course I have private variable in the header file to declare the various variables. Three QPushButton, one QWidget and one QTextEdit. And when I did this it will always crash when running on the layout setting with a SIGSEGV error.
If I change the line which has the problem by the following :
@QTextEdit *m_teLog = new QTextEdit;@Then it just works perfectly. And that's where I don't get what is wrong because it's basically exactly thesame code, only on one case the variables is declared locally inside the function, which I think is not correct as I'll need to use that component elsewhere, but as far as I know both way should give the same result but here one is working and the other crash the application.
I guess there must be a specific that I've missed.
Aurelien
-
Hi and welcome to devnet,
What exact error do you get ?
-
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 ?
-
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