Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Program crashes when global variable included.



  • When i add a global variable in my function, the program will crash when running. Is there a way to replace global variable to something else? or reason why it will crash?

    Below are the codes:
    I added g_y as a global variable.

    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);
        populateThemeBox();
        populateAnimationBox();
        populateLegendBox();
        //create charts
        QChartView *chartView;
    
    ThemeWidget::~ThemeWidget()
    {
        delete m_ui;
    }
    DataTable ThemeWidget::generateRandomData(const QByteArray &data)
    {
        DataTable dataTable;
        QByteArray testdata = data;
         qDebug() << testdata << "hi";
        g_y = 3;
        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;
    }```
    code_text
    

  • Lifetime Qt Champion

    Please use code-tags so we can read your code. Also when the program crashes you should use a debugger to see where exactly it crashes.
    Global variables should be avoided.



  • 1c6d96c1-0b99-4ebb-872e-63d4737c5853-image.png

    This is the error. Whats the recommended way to solve this problem?


  • Lifetime Qt Champion

    @jbbb

    Hi
    Normally one can see what it was doing in Creator, in the call stack

    alt text

    make sure you run app in debug mode.

    If you think its crashing in
    DataTable ThemeWidget::generateRandomData(const QByteArray &data)

    then add break there
    and then single step line by line to see exact spot.



  • @mrjj yes, the error is with the global variable. is there any other methods i can use to replace global variable?

    I'm looking to use global variable because DataTable ThemeWidget::generateRandomData(const QByteArray &data) is actually a slot.

    to store the values in my datatable m_dataTable(generateRandomData(m_data)),

    i will need to have m_data as the slot have an argument.

    any recommendation?


  • Lifetime Qt Champion

    Hi
    But is m_dataTable part of ThemeWidget?
    Then the generateRandomData can simply access it ?
    Since they are both members ?



  • @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;
    
    };
    

  • Lifetime Qt Champion

    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...!



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


  • Lifetime Qt Champion

    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.



  • @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.


  • Lifetime Qt Champion

    @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.



  • @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.


  • Lifetime Qt Champion

    @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 ?



  • @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!


  • Lifetime Qt Champion

    @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 ?



  • @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.


  • Lifetime Qt Champion

    @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.


  • Moderators

    @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-Hilk how do i set the global variable instead? not in .h file?



  • @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?


  • Lifetime Qt Champion

    @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.


Log in to reply