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. Program crashes when global variable included.
Forum Updated to NodeBB v4.3 + New Features

Program crashes when global variable included.

Scheduled Pinned Locked Moved Unsolved General and Desktop
22 Posts 4 Posters 6.3k 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.
  • Christian EhrlicherC Christian Ehrlicher

    Again: lease use code-tags so we can read your code. Please take a look at your post and think about if others can read it...!

    J Offline
    J Offline
    jbbb
    wrote on last edited by
    #9

    @Christian-Ehrlicher hi, so sorry! Tried using it just now but it couldnt work. Made adjustment alr.

    1 Reply Last reply
    0
    • J jbbb

      @mrjj this is my .h file. initialized here means member right?

      #ifndef THEMEWIDGET_H
      #define THEMEWIDGET_H
      
      #include <QtWidgets/QWidget>
      #include <QtCharts/QChartGlobal>
      #include <QPlainTextEdit>
      
      QT_BEGIN_NAMESPACE
      class QComboBox;
      class QCheckBox;
      class Ui_ThemeWidgetForm;
      //class QString;
      QT_END_NAMESPACE
      
      QT_CHARTS_BEGIN_NAMESPACE
      class QChartView;
      class QChart;
      QT_CHARTS_END_NAMESPACE
      
      typedef QPointF Data;
      typedef QList<Data> DataList;
      typedef QList<DataList> DataTable;
      
      QT_CHARTS_USE_NAMESPACE
      
      class ThemeWidget: public QWidget
      {
          Q_OBJECT
      public:
          explicit ThemeWidget(QWidget *parent = 0);
          ~ThemeWidget();
           DataTable generateRandomData(const QByteArray &data);     
      
      private Q_SLOTS:
          void updateUI();
      private:
          void populateThemeBox();
          void populateAnimationBox();
          void populateLegendBox();
          void connectSignals();
          QChart *createLineChart() const;
          void init_LineChart(QChart *chart, QString ChartName) const;
      
      
      private:
          int m_listCount;
          int m_valueMax;
          float m_valueCount;
          QList<QChartView *> m_charts;
          DataTable m_dataTable;
          QByteArray m_data;
          int g_y { 1 };
          QChart *hs_ch1_raw_chart;
          QChart *hs_ch1_fft_chart;
          QChart *hs_ch2_raw_chart;
          QChart *hs_ch2_fft_chart;
      
      
          Ui_ThemeWidgetForm *m_ui;
      
      };
      
      mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by mrjj
      #10

      Hi
      Well yes, one can say if in .h its a member but it has to be inside the
      class definition.

      class SomeWidget: public QWidget
      {
      Int Im_A_member;
      void SomeMemeberFunction();
      };

      So yes m_dataTable is class member :)

      private:
      ...
      DataTable m_dataTable;

      and that means that any member functions can directly read it.
      (like SomeMemeberFunction here )
      or
      DataTable generateRandomData(const QByteArray &data);
      in your case. So it doesn't need to have it as a parameter as they are part of the same class and it can use all.

      J 1 Reply Last reply
      0
      • mrjjM mrjj

        Hi
        Well yes, one can say if in .h its a member but it has to be inside the
        class definition.

        class SomeWidget: public QWidget
        {
        Int Im_A_member;
        void SomeMemeberFunction();
        };

        So yes m_dataTable is class member :)

        private:
        ...
        DataTable m_dataTable;

        and that means that any member functions can directly read it.
        (like SomeMemeberFunction here )
        or
        DataTable generateRandomData(const QByteArray &data);
        in your case. So it doesn't need to have it as a parameter as they are part of the same class and it can use all.

        J Offline
        J Offline
        jbbb
        wrote on last edited by
        #11

        @mrjj This means that I dont need a parameter in my m_dataTable(generateRandomData(m_data)) or i dont need the parameter in my slot function?

        Sorry, I am abit confused. I understand that I have declared the members. But if i were to remove m_data, there will be a syntax error.

        mrjjM 1 Reply Last reply
        0
        • J jbbb

          @mrjj This means that I dont need a parameter in my m_dataTable(generateRandomData(m_data)) or i dont need the parameter in my slot function?

          Sorry, I am abit confused. I understand that I have declared the members. But if i were to remove m_data, there will be a syntax error.

          mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on last edited by
          #12

          @jbbb
          Hi
          well QByteArray m_data is a member too so you can just use it in the function

          However, your current code use it as a data as that is what parameter is named so
          the code in the function can use m_data instead of the data;

          DataTable ThemeWidget::generateRandomData(const QByteArray &data)
          {
          QByteArray testdata = data; <<< uses data

          so if you had
          DataTable ThemeWidget::generateRandomData()
          {
          QByteArray testdata = m_data; // uses the member

          ...
          But you can do
          m_dataTable(generateRandomData(m_data))

          if you want but its maybe a bit silly to give it as a parameter when its a member but it could accept
          other datas that way so could be fine as later you might call it with another Qbyetarray so maybe
          the parameter is fine if that would ever be needed.

          J 1 Reply Last reply
          0
          • mrjjM mrjj

            @jbbb
            Hi
            well QByteArray m_data is a member too so you can just use it in the function

            However, your current code use it as a data as that is what parameter is named so
            the code in the function can use m_data instead of the data;

            DataTable ThemeWidget::generateRandomData(const QByteArray &data)
            {
            QByteArray testdata = data; <<< uses data

            so if you had
            DataTable ThemeWidget::generateRandomData()
            {
            QByteArray testdata = m_data; // uses the member

            ...
            But you can do
            m_dataTable(generateRandomData(m_data))

            if you want but its maybe a bit silly to give it as a parameter when its a member but it could accept
            other datas that way so could be fine as later you might call it with another Qbyetarray so maybe
            the parameter is fine if that would ever be needed.

            J Offline
            J Offline
            jbbb
            wrote on last edited by
            #13

            @mrjj Hi, ! cant remove my parameters as it is a signal and slot.

            At my main mindow(signal):

            void MainWindow::themereadData(const QByteArray &data)
            {
                  m_themewidget->generateRandomData(data);
            }
            

            hence when at ThemeWidget.cpp:

            DataTable ThemeWidget::generateRandomData(const QByteArray &data)
            

            my function must have &data for the data to enter this .cpp

            Not quite sure how it can be done although i understand your point about the members.

            mrjjM 1 Reply Last reply
            0
            • J jbbb

              @mrjj Hi, ! cant remove my parameters as it is a signal and slot.

              At my main mindow(signal):

              void MainWindow::themereadData(const QByteArray &data)
              {
                    m_themewidget->generateRandomData(data);
              }
              

              hence when at ThemeWidget.cpp:

              DataTable ThemeWidget::generateRandomData(const QByteArray &data)
              

              my function must have &data for the data to enter this .cpp

              Not quite sure how it can be done although i understand your point about the members.

              mrjjM Offline
              mrjjM Offline
              mrjj
              Lifetime Qt Champion
              wrote on last edited by
              #14

              @jbbb

              Ah ok. well then it needs the parameter.
              I think the code confused me as you seemed to send a member as parameter but
              now it makes sense.

              So did you find the actual crashing ?

              J 1 Reply Last reply
              0
              • mrjjM mrjj

                @jbbb

                Ah ok. well then it needs the parameter.
                I think the code confused me as you seemed to send a member as parameter but
                now it makes sense.

                So did you find the actual crashing ?

                J Offline
                J Offline
                jbbb
                wrote on last edited by
                #15

                @mrjj There is a segmentation fault when i input a global variable into the function below.

                DataTable ThemeWidget::generateRandomData(const QByteArray &data)
                {
                    DataTable dataTable;
                    QByteArray testdata = data;
                     qDebug() << testdata << "hi";
                    QString headerbyte = testdata.left(10);
                    testdata.remove(0,10);
                    // generate testdata
                    int listCount = 1;
                    for (int i(0); i < listCount; i++) {
                        DataList dataList;
                        float yValue(0);
                        int count(0);
                        QString datavalue;
                        QString checksign;
                        for (int j(0); j<136; j++)
                        {
                            testdata.remove(0,1);
                            checksign = testdata.left(1);
                            if (checksign == "-"){
                                datavalue = testdata.left(7);
                                testdata.remove(0,7);
                            }
                            else{
                                datavalue = testdata.left(6);
                                testdata.remove(0,6);
                            }
                            yValue = datavalue.toFloat();
                            QPointF value(count, yValue);
                            count++;
                            dataList << Data(value);
                        }
                        dataTable << dataList;
                    }
                    return dataTable;
                }
                

                the reason why i will need a global variable is because of:
                m_dataTable(generateRandomData(m_data))

                As this function is a return function, I will need a variable to input to the function. However, &data in my function is my actual data. Hence, I wanted to use m_data to be pointing to data so that m_data will have the same data stored.

                I am not sure of the reason why there will be a fault. Hope you can enlighten me!

                mrjjM 1 Reply Last reply
                0
                • J jbbb

                  @mrjj There is a segmentation fault when i input a global variable into the function below.

                  DataTable ThemeWidget::generateRandomData(const QByteArray &data)
                  {
                      DataTable dataTable;
                      QByteArray testdata = data;
                       qDebug() << testdata << "hi";
                      QString headerbyte = testdata.left(10);
                      testdata.remove(0,10);
                      // generate testdata
                      int listCount = 1;
                      for (int i(0); i < listCount; i++) {
                          DataList dataList;
                          float yValue(0);
                          int count(0);
                          QString datavalue;
                          QString checksign;
                          for (int j(0); j<136; j++)
                          {
                              testdata.remove(0,1);
                              checksign = testdata.left(1);
                              if (checksign == "-"){
                                  datavalue = testdata.left(7);
                                  testdata.remove(0,7);
                              }
                              else{
                                  datavalue = testdata.left(6);
                                  testdata.remove(0,6);
                              }
                              yValue = datavalue.toFloat();
                              QPointF value(count, yValue);
                              count++;
                              dataList << Data(value);
                          }
                          dataTable << dataList;
                      }
                      return dataTable;
                  }
                  

                  the reason why i will need a global variable is because of:
                  m_dataTable(generateRandomData(m_data))

                  As this function is a return function, I will need a variable to input to the function. However, &data in my function is my actual data. Hence, I wanted to use m_data to be pointing to data so that m_data will have the same data stored.

                  I am not sure of the reason why there will be a fault. Hope you can enlighten me!

                  mrjjM Offline
                  mrjjM Offline
                  mrjj
                  Lifetime Qt Champion
                  wrote on last edited by
                  #16

                  @jbbb

                  Well global variables should normally be avoided as it gives a spaghetti design but
                  it should not crash as such.

                  Do notice that no Qt-based (QObject) class can be used as global variable. Its forbidden.

                  So what type was your global variable?

                  and why did you need it to be global ?

                  J 1 Reply Last reply
                  0
                  • mrjjM mrjj

                    @jbbb

                    Well global variables should normally be avoided as it gives a spaghetti design but
                    it should not crash as such.

                    Do notice that no Qt-based (QObject) class can be used as global variable. Its forbidden.

                    So what type was your global variable?

                    and why did you need it to be global ?

                    J Offline
                    J Offline
                    jbbb
                    wrote on last edited by
                    #17

                    @mrjj

                    #include "themewidget.h"
                    #include "ui_themewidget.h"
                    #include "tcpserver.h"
                    
                    #include <iostream>
                    #include <QtCharts/QChartView>
                    #include <QtCharts/QLineSeries>
                    #include <QtCharts/QLegend>
                    #include <QtWidgets/QGridLayout>
                    #include <QtWidgets/QFormLayout>
                    #include <QtWidgets/QComboBox>
                    #include <QtWidgets/QSpinBox>
                    #include <QtWidgets/QCheckBox>
                    #include <QtWidgets/QGroupBox>
                    #include <QtWidgets/QLabel>
                    #include <QtCore/QRandomGenerator>
                    #include <QtCharts/QBarCategoryAxis>
                    #include <QtWidgets/QApplication>
                    #include <QtCharts/QValueAxis>
                    
                    ThemeWidget::ThemeWidget(QWidget *parent) :
                        QWidget(parent),
                        m_listCount(1),
                        m_valueMax(136),
                        m_valueCount(0.5),   
                        m_dataTable(generateRandomData(m_data)),
                        m_dataTable1(generateDataTest()),
                        m_ui(new Ui_ThemeWidgetForm)
                        //m_dataTable(generateRandomData(m_listCount, m_valueMax, m_valueCount)),
                    {
                        m_ui->setupUi(this);
                        //create charts
                        QChartView *chartView;
                    
                    ThemeWidget::~ThemeWidget()
                    {
                        delete m_ui;
                    }
                    DataTable ThemeWidget::generateRandomData(const QByteArray &data, QByteArray*pData)
                    {
                        DataTable dataTable;
                         QByteArray testdata = data;
                        m_data = testdata;
                        QString headerbyte = testdata.left(10);
                        testdata.remove(0,10);
                        // generate testdata
                        int listCount = 1;
                        for (int i(0); i < listCount; i++) {
                            DataList dataList;
                            float yValue(0);
                            int count(0);
                            QString datavalue;
                            QString checksign;
                            for (int j(0); j<136; j++)
                            {
                                testdata.remove(0,1);
                                checksign = testdata.left(1);
                                if (checksign == "-"){
                                    datavalue = testdata.left(7);
                                    testdata.remove(0,7);
                                }
                                else{
                                    datavalue = testdata.left(6);
                                    testdata.remove(0,6);
                                }
                                yValue = datavalue.toFloat();
                                QPointF value(count, yValue);
                                count++;
                                dataList << Data(value);
                            }
                            dataTable << dataList;
                        }
                        return dataTable;
                    }
                    

                    This is the codes that will create a segmentation fault. My global variable is
                    QByteArray m_data that is initialized in the .header file.

                    m_data = testdata;

                    this is so that m_data for m_dataTable(generateRandomData(m_data)) will have the same data other wise m_data will be empty.

                    mrjjM J.HilkJ 2 Replies Last reply
                    0
                    • J jbbb

                      @mrjj

                      #include "themewidget.h"
                      #include "ui_themewidget.h"
                      #include "tcpserver.h"
                      
                      #include <iostream>
                      #include <QtCharts/QChartView>
                      #include <QtCharts/QLineSeries>
                      #include <QtCharts/QLegend>
                      #include <QtWidgets/QGridLayout>
                      #include <QtWidgets/QFormLayout>
                      #include <QtWidgets/QComboBox>
                      #include <QtWidgets/QSpinBox>
                      #include <QtWidgets/QCheckBox>
                      #include <QtWidgets/QGroupBox>
                      #include <QtWidgets/QLabel>
                      #include <QtCore/QRandomGenerator>
                      #include <QtCharts/QBarCategoryAxis>
                      #include <QtWidgets/QApplication>
                      #include <QtCharts/QValueAxis>
                      
                      ThemeWidget::ThemeWidget(QWidget *parent) :
                          QWidget(parent),
                          m_listCount(1),
                          m_valueMax(136),
                          m_valueCount(0.5),   
                          m_dataTable(generateRandomData(m_data)),
                          m_dataTable1(generateDataTest()),
                          m_ui(new Ui_ThemeWidgetForm)
                          //m_dataTable(generateRandomData(m_listCount, m_valueMax, m_valueCount)),
                      {
                          m_ui->setupUi(this);
                          //create charts
                          QChartView *chartView;
                      
                      ThemeWidget::~ThemeWidget()
                      {
                          delete m_ui;
                      }
                      DataTable ThemeWidget::generateRandomData(const QByteArray &data, QByteArray*pData)
                      {
                          DataTable dataTable;
                           QByteArray testdata = data;
                          m_data = testdata;
                          QString headerbyte = testdata.left(10);
                          testdata.remove(0,10);
                          // generate testdata
                          int listCount = 1;
                          for (int i(0); i < listCount; i++) {
                              DataList dataList;
                              float yValue(0);
                              int count(0);
                              QString datavalue;
                              QString checksign;
                              for (int j(0); j<136; j++)
                              {
                                  testdata.remove(0,1);
                                  checksign = testdata.left(1);
                                  if (checksign == "-"){
                                      datavalue = testdata.left(7);
                                      testdata.remove(0,7);
                                  }
                                  else{
                                      datavalue = testdata.left(6);
                                      testdata.remove(0,6);
                                  }
                                  yValue = datavalue.toFloat();
                                  QPointF value(count, yValue);
                                  count++;
                                  dataList << Data(value);
                              }
                              dataTable << dataList;
                          }
                          return dataTable;
                      }
                      

                      This is the codes that will create a segmentation fault. My global variable is
                      QByteArray m_data that is initialized in the .header file.

                      m_data = testdata;

                      this is so that m_data for m_dataTable(generateRandomData(m_data)) will have the same data other wise m_data will be empty.

                      mrjjM Offline
                      mrjjM Offline
                      mrjj
                      Lifetime Qt Champion
                      wrote on last edited by
                      #18

                      @jbbb

                      well it might be a dangling pointer maybe ?

                      You can find out with the debugger.

                      Place a break point on generateRandomData and then
                      use F10 to spep over each line and see what lines it crashes at.

                      J 1 Reply Last reply
                      0
                      • J jbbb

                        @mrjj

                        #include "themewidget.h"
                        #include "ui_themewidget.h"
                        #include "tcpserver.h"
                        
                        #include <iostream>
                        #include <QtCharts/QChartView>
                        #include <QtCharts/QLineSeries>
                        #include <QtCharts/QLegend>
                        #include <QtWidgets/QGridLayout>
                        #include <QtWidgets/QFormLayout>
                        #include <QtWidgets/QComboBox>
                        #include <QtWidgets/QSpinBox>
                        #include <QtWidgets/QCheckBox>
                        #include <QtWidgets/QGroupBox>
                        #include <QtWidgets/QLabel>
                        #include <QtCore/QRandomGenerator>
                        #include <QtCharts/QBarCategoryAxis>
                        #include <QtWidgets/QApplication>
                        #include <QtCharts/QValueAxis>
                        
                        ThemeWidget::ThemeWidget(QWidget *parent) :
                            QWidget(parent),
                            m_listCount(1),
                            m_valueMax(136),
                            m_valueCount(0.5),   
                            m_dataTable(generateRandomData(m_data)),
                            m_dataTable1(generateDataTest()),
                            m_ui(new Ui_ThemeWidgetForm)
                            //m_dataTable(generateRandomData(m_listCount, m_valueMax, m_valueCount)),
                        {
                            m_ui->setupUi(this);
                            //create charts
                            QChartView *chartView;
                        
                        ThemeWidget::~ThemeWidget()
                        {
                            delete m_ui;
                        }
                        DataTable ThemeWidget::generateRandomData(const QByteArray &data, QByteArray*pData)
                        {
                            DataTable dataTable;
                             QByteArray testdata = data;
                            m_data = testdata;
                            QString headerbyte = testdata.left(10);
                            testdata.remove(0,10);
                            // generate testdata
                            int listCount = 1;
                            for (int i(0); i < listCount; i++) {
                                DataList dataList;
                                float yValue(0);
                                int count(0);
                                QString datavalue;
                                QString checksign;
                                for (int j(0); j<136; j++)
                                {
                                    testdata.remove(0,1);
                                    checksign = testdata.left(1);
                                    if (checksign == "-"){
                                        datavalue = testdata.left(7);
                                        testdata.remove(0,7);
                                    }
                                    else{
                                        datavalue = testdata.left(6);
                                        testdata.remove(0,6);
                                    }
                                    yValue = datavalue.toFloat();
                                    QPointF value(count, yValue);
                                    count++;
                                    dataList << Data(value);
                                }
                                dataTable << dataList;
                            }
                            return dataTable;
                        }
                        

                        This is the codes that will create a segmentation fault. My global variable is
                        QByteArray m_data that is initialized in the .header file.

                        m_data = testdata;

                        this is so that m_data for m_dataTable(generateRandomData(m_data)) will have the same data other wise m_data will be empty.

                        J.HilkJ Offline
                        J.HilkJ Offline
                        J.Hilk
                        Moderators
                        wrote on last edited by
                        #19

                        @jbbb said in Program crashes when global variable included.:

                        My global variable is
                        QByteArray m_data that is initialized in the .header file.

                        m_data is not a global variable, its member one

                        private:
                            int m_listCount;
                            int m_valueMax;
                            float m_valueCount;
                            QList<QChartView *> m_charts;
                            DataTable m_dataTable;
                            QByteArray m_data;
                        

                        Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                        Q: What's that?
                        A: It's blue light.
                        Q: What does it do?
                        A: It turns blue.

                        J 1 Reply Last reply
                        0
                        • J.HilkJ J.Hilk

                          @jbbb said in Program crashes when global variable included.:

                          My global variable is
                          QByteArray m_data that is initialized in the .header file.

                          m_data is not a global variable, its member one

                          private:
                              int m_listCount;
                              int m_valueMax;
                              float m_valueCount;
                              QList<QChartView *> m_charts;
                              DataTable m_dataTable;
                              QByteArray m_data;
                          
                          J Offline
                          J Offline
                          jbbb
                          wrote on last edited by
                          #20

                          @J-Hilk how do i set the global variable instead? not in .h file?

                          1 Reply Last reply
                          0
                          • mrjjM mrjj

                            @jbbb

                            well it might be a dangling pointer maybe ?

                            You can find out with the debugger.

                            Place a break point on generateRandomData and then
                            use F10 to spep over each line and see what lines it crashes at.

                            J Offline
                            J Offline
                            jbbb
                            wrote on last edited by jbbb
                            #21

                            @mrjj it crashes at m_data = testdata;

                            and this pops out:

                            QByteArray &QByteArray::operator=(const QByteArray & other) Q_DECL_NOTHROW
                            {
                                other.d->ref.ref();
                                if (!d->ref.deref())
                                    Data::deallocate(d);
                                d = other.d;
                                return *this;
                            }
                            

                            any idea what this means?

                            1 Reply Last reply
                            0
                            • Christian EhrlicherC Offline
                              Christian EhrlicherC Offline
                              Christian Ehrlicher
                              Lifetime Qt Champion
                              wrote on last edited by
                              #22

                              @jbbb said in Program crashes when global variable included.:

                              any idea what this means?

                              Take a look at the stacktrace to see where it comes from (as already said in my first post).
                              I would guess the this pointer is a nullptr.

                              Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                              Visit the Qt Academy at https://academy.qt.io/catalog

                              1 Reply Last reply
                              3

                              • Login

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