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
Forum Updated to NodeBB v4.3 + New Features

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

Scheduled Pinned Locked Moved General and Desktop
14 Posts 3 Posters 6.0k Views 1 Watching
  • 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
    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