segmentation fault
-
i call it in mainwindow.cpp file.
here i have send the code snip for that portion.
mainwindow.cpp
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { ui->setupUi(this); ui->send->setDisabled(true); ui->message->setDisabled(true); ui->subscribe->setDisabled(true); ui->topic->setDisabled(true); ui->lineIP->setText(QString("a3jkuztr3frwt4-ats.iot.ap-south-1.amazonaws.com")); ui->linePort->setText(QString("8883")); lib_init(); connect (ui->connect, SIGNAL(clicked()), this, SLOT(connectPressed())); connect (ui->send, SIGNAL(clicked()), this, SLOT(sendPressed())); connect (ui->subscribe, SIGNAL(clicked()), this, SLOT(subscribePressed())); } void MainWindow::connectPressed() { QByteArray host = ui->lineIP->text().toLocal8Bit(); QByteArray id = ui->lineID->text().toLocal8Bit(); int port = 0; port = ui->linePort->text().toInt(); if (!port) { ui->linePort->clear(); return ; } ui->connect->setDisabled(true); mosq = new qtmosq(id, true); int size = sizeof(*mosq); qDebug() << size; connect (mosq, SIGNAL(connectEnable()), this, SLOT(connectEnabled())); connect (mosq, SIGNAL(subscribed()), this ,SLOT(subscribed())); connect (mosq, SIGNAL(connected()), this, SLOT(connected())); connect (mosq, SIGNAL(messageSent(bool)), this, SLOT(setMessageStatus(bool))); connect (mosq, SIGNAL(messageReceived(QString)), this, SLOT(showMessage(QString))); mosq->connect_async(host.data(), port); mosq->loop_start(); }
mainwindow.h
#include <QMainWindow> #include "qtmosq.h" namespace Ui { class MainWindow; } class MainWindow : public QMainWindow { Q_OBJECT public: explicit MainWindow(QWidget *parent = 0); ~MainWindow(); private: qtmosq* mosq; QStringList subed; QString currentTopic; Ui::MainWindow *ui; private slots: void setMessageStatus(bool); void connectPressed(); void sendPressed(); void connectEnabled(); void subscribePressed(); void subscribed(); void connected(); void showMessage(QString); };
qtmosq.h
#ifndef QTMOSQ_H #define QTMOSQ_H #include "QObject" #include "mosquittopp.h" using namespace mosqpp; class qtmosq : public QObject, public mosquittopp { Q_OBJECT public: qtmosq(const char *id = NULL, bool clean_session = true) : mosquittopp (id, clean_session) {MID = 0;} ~qtmosq() {} void on_connect(int result) { if (!result) { //subscribe(NULL, "$SYS/#", 2); emit connected(); } else emit connectEnable(); } void on_publish(int id) { MID++; emit messageSent(true); } void on_subscribe(int mid, int qos_count, const int *granted_qos) { if (MID != 0) emit subscribed(); MID++; } void on_message(const mosquitto_message *message) { if (message->payloadlen) { QString s(message->topic); s += '>'; s += (char*) message->payload; emit messageReceived(s); } } int* getMID() {return &MID;} private: int MID; signals: void connected(); void messageSent(bool); void messageReceived(QString); void connectEnable(); void subscribed(); }; #endif // QTMOSQ_H
here above i send the code for the same.
if you get more please tell me.
-
@Milav So, you get that error here in MainWindow:
ui->connect->setDisabled(true); mosq = new qtmosq(id, true); // Is it here? int size = sizeof(*mosq);
?
It should work.
Are there any other warnings or errors?
Try to do a complete rebuild: delete build directory, run qmake and build. -
when i build the project it give me only warning like follow.
/home/tdp0009/Teq_Projects/Aug_2019/HMI/Firmware/QT/12_oct_hmi/qt-mosquitto-master/qtmosq.h:14: warning: 'mosqpp::mosquittopp::mosquittopp(const char*, bool)' is deprecated (declared at ../../../../CC_Lib/mosquitto/phytec_wega_build_websockets/usr/local/include/mosquittopp.h:94) [-Wdeprecated-declarations] /home/tdp0009/Teq_Projects/Aug_2019/HMI/Firmware/QT/12_oct_hmi/qt-mosquitto-master/mainwindow.cpp:57: warning: 'int mosqpp::mosquittopp::connect_async(const char*, int, int)' is deprecated (declared at ../../../../CC_Lib/mosquitto/phytec_wega_build_websockets/usr/local/include/mosquittopp.h:103) [-Wdeprecated-declarations] /home/tdp0009/Teq_Projects/Aug_2019/HMI/Firmware/QT/12_oct_hmi/qt-mosquitto-master/mainwindow.cpp:80: warning: 'int mosqpp::mosquittopp::subscribe(int*, const char*, int)' is deprecated (declared at ../../../../CC_Lib/mosquitto/phytec_wega_build_websockets/usr/local/include/mosquittopp.h:110) [-Wdeprecated-declarations]
but i face segmentation fault error when i run that project, an i found the line at where i face segmentation fault.
it is ...
mosq->subscribe(NULL, "sensors/temprature", 1); // Segmentation falut
-
@Milav That example does exactly what I suggested you to do:
ui->connect->setDisabled(true); mosq = new qtmosq(id, false); // HERE connect (mosq, SIGNAL(connectEnable()), this, SLOT(connectEnabled()));
Did you try to build and run this example? If so - does it work?
-
@Milav said in segmentation fault:
it is not work
What exactly does this mean? Doesn't build? Crashes? Does not do what it should? ...?
-
it means build successful.
but it is not run, it is crashes application.
means while running the application, the application are crash.
with following error.
"segmentation fault"
and i debug the application via qDebug(), the application are crash on following line of code.
mosq->subscribe(NULL, "sensors/temprature", 1); // Segmentation falut
-
@Milav Did you debug to see whether mosq is not a nullptr? If it's not please provide stack trace after crash.
Also, in connectPressed it does a return if port is not set - is it set if you press connect?
You should really run through debugger... -
i debug the size of mosq pointer through following code.
int size = sizeof(*mosq); qDebug() << size;
and output is 20;
and in connectpressed button slot are run succesfully.
port value are shown correctly.
when i run debugger it shows following error.
The inferior stopped because it received a signal from the Operating System. Signal name : SIGILL Signal meaning : Illegal instruction
hope you got my point.
-
@Milav said in segmentation fault:
i debug the size of mosq pointer through following code.
The size of a pointer is fixed, it doesn't matter whether it contains nullptr or something else.
DoqDebug() << mosq;
instead to see the value of the pointer.
Without stack trace I can't tell you what is going on... -
int size = sizeof(*mosq);
The size of a pointer is fixed,
A slight slip of the tongue there. I think what my colleague @jsulm meant to clarify is:
sizeof(...)
is a compile-time calculation, it does not matter what the value ofmosq
is or what it points at run-time, rather this statement just assignssizeof(qtmosq)
regardless. As he says, show us what is in themosq
. -
when i execute "qDebug() << mosq;" it gives following output.
qtmosq(0xac50b0)I'm not sure this is actually trying to deference the pointer, it may just print its value. I would try something like
qDebug() << mosq->something;
, wheresomething
is some field you know is supposed to exist & be valid in aqtmosq
. Or tryqDebug() << *mosq
(note the*
)?how i see stack trace in qt?
can you please explore me more about stack trace?I don't know how you run in your "Embedded Custom Board", but can you run the app from inside a debugger? then when it seg faults you will see a "stack trace" in the debugger showing the call stack when the fault occurred.