Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [SOLVED]Variable declared private in class cause crash while declared locally it works

[SOLVED]Variable declared private in class cause crash while declared locally it works

Scheduled Pinned Locked Moved General and Desktop
14 Posts 3 Posters 5.8k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    alarai
    wrote on last edited by
    #1

    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

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi and welcome to devnet,

      What exact error do you get ?

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • A Offline
        A Offline
        alarai
        wrote on last edited by
        #3

        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.

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          Looks like some answers disappeared, did you got the others ?

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0
          • A Offline
            A Offline
            arsinte_andrei
            wrote on last edited by
            #5
            • 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
            1 Reply Last reply
            0
            • A Offline
              A Offline
              alarai
              wrote on last edited by
              #6

              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_OBJECT

              public:
              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();
              

              }@

              1 Reply Last reply
              0
              • SGaistS Offline
                SGaistS Offline
                SGaist
                Lifetime Qt Champion
                wrote on last edited by
                #7

                Are you running on windows ?

                Interested in AI ? www.idiap.ch
                Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                1 Reply Last reply
                0
                • A Offline
                  A Offline
                  alarai
                  wrote on last edited by
                  #8

                  yes it's on windows and with mingw compiler.

                  1 Reply Last reply
                  0
                  • A Offline
                    A Offline
                    arsinte_andrei
                    wrote on last edited by
                    #9

                    can you show as how is your main.cpp do you set any layout there?

                    1 Reply Last reply
                    0
                    • A Offline
                      A Offline
                      alarai
                      wrote on last edited by
                      #10

                      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();
                      

                      }@

                      1 Reply Last reply
                      0
                      • A Offline
                        A Offline
                        arsinte_andrei
                        wrote on last edited by
                        #11

                        I can not see mainWin.show();
                        Also is a type error that is doubled or you just have it like that?

                        1 Reply Last reply
                        0
                        • A Offline
                          A Offline
                          alarai
                          wrote on last edited by
                          #12

                          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.

                          1 Reply Last reply
                          0
                          • A Offline
                            A Offline
                            arsinte_andrei
                            wrote on last edited by
                            #13

                            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

                            1 Reply Last reply
                            0
                            • A Offline
                              A Offline
                              alarai
                              wrote on last edited by
                              #14

                              yep just did it thanks a lot for the help :)

                              1 Reply Last reply
                              0

                              • Login

                              • Login or register to search.
                              • First post
                                Last post
                              0
                              • Categories
                              • Recent
                              • Tags
                              • Popular
                              • Users
                              • Groups
                              • Search
                              • Get Qt Extensions
                              • Unsolved