Unsolved Program crashes when global variable included.
-
@Christian-Ehrlicher hi, so sorry! Tried using it just now but it couldnt work. Made adjustment alr.
-
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.
-
@jbbb
Hi
well QByteArray m_data is a member too so you can just use it in the functionHowever, 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 dataso 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.
-
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!
-
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 ?
-
#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.
-
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. -
@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?
-
@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 thethis
pointer is anullptr
.