segmentation fault



  • Hello,

    I make a qt application for Embedded Custom Board and when i run the Application it gives segmentation fault.

    The application give segmentation fault due to following line.

      mosq->subscribe(mosq->getMID(), topic.data(), 1);
    

    where mosq is define and declare as follow.

    class MainWindow : public QMainWindow
    {
        Q_OBJECT
    
    public:
        explicit MainWindow(QWidget *parent = 0);
        ~MainWindow();
    
    private:
        qtmosq* mosq = NULL;
        QStringList subed;
        QString currentTopic;
        Ui::MainWindow *ui;
    --------
    --------
    --------
    };
    

    where qtmosq is define as follow..

    #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++;
        }
    ..........
    ..........
    ..........
    
    signals:
        void connected();
        void messageSent(bool);
        void messageReceived(QString);
        void connectEnable();
        void subscribed();
    };
    

    so what is the reason behind for this fault.

    The same application are successfully run on Host Linux PC.

    I have use same mosquitto library in both the test.

    please help me to sort out this problem.

    I have send the reference link at where i used above program.

    Link :- Ref. Link

    Thank you
    Milav Soni



  • @Milav said in segmentation fault:

    The application give segmentation fault due to following line.
    mosq->subscribe(mosq->getMID(), topic.data(), 1);

    Little information in your post. But likely one of:

    • mosq is nullptr/bad pointer
    • mosq->subscribe() or mosq->getMID() are raising the SEGV
    • topic.data() is raising SEGV

    You're using some external library code here, so who knows what that might do.
    Can't you run under debugger and get more specific traceback for the SEGV?



  • @JonB said in segmentation fault:

    nullptr

    Thank you for your response.

    Debugger is not run, it gives following error.

    The inferior stopped because it received a signal from the Operating System.
    
    Signal name : SIGILL
    Signal meaning : Illegal instruction
    
    

    And i replace NULL value in mosq->getMID() ,
    i replace Topic Name direct instead of topic.data()

    but i face same problem.

    so may be it can be happen by

    1. mosq
    2. mosq->subscribe()

    so, is there any another way to solve this error.

    you tell me mosq is bad pointer, so what is best way to make it good pointer.

    is i used memory allocation in mosq defination?

    please help me to find out this error solution.

    Thank you
    MilaV Soni


  • Qt Champions 2018

    @Milav said in segmentation fault:

    you tell me mosq is bad pointer, so what is best way to make it good pointer.

    Create an instance and assign the pointer:

    mosq = new qtmosq();
    


  • @jsulm

    i tried with above syntax.

    qtmosq* mosq;
     mosq = new qtmosq(id, true);
    

    but still i am face same segmentation fault error.

    can you please give me best solution for the same.


  • Qt Champions 2018

    @Milav said in segmentation fault:

    but still i am face same segmentation fault error.

    Yes, because you now create a new local variable called mosq which is completely unrelated to mosq in MainWindow! So, the one in MainWindow remains a NULL pointer.
    Remove

    qtmosq* mosq;
    


  • @jsulm

    But than it give error like

    /home/tdp0009/Teq_Projects/Aug_2019/HMI/Firmware/QT/12_oct_hmi/qt-mosquitto-master/mainwindow.cpp:47: error: 'mosq' was not declared in this scope
    

    Can i declare any another place like mainwindow.h?
    i also tried in mainwindow.h

    like following...

    private:
        qtmosq* mosq;
        QStringList subed;
        QString currentTopic;
        Ui::MainWindow *ui;
    
    

    but it gives same error.


  • Qt Champions 2018

    @Milav Please show where exactly you're calling

    mosq = new qtmosq();
    

    It must be inside one of the MainWindow methods.



  • @jsulm

    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.


  • Qt Champions 2018

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



  • @jsulm

    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
    


  • @jsulm

    Hello

    Here i have send the link at where i download the example.

    Reference link


  • Qt Champions 2018

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



  • @jsulm

    Yes i delete the build directory and again i rebuild the whole project but i couldn't run the example. it is not work.


  • Qt Champions 2018

    @Milav said in segmentation fault:

    it is not work

    What exactly does this mean? Doesn't build? Crashes? Does not do what it should? ...?



  • @jsulm

    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
    

  • Moderators

    @Milav well, did you press connect before you pressed subscribe?

    If not it will crash...



  • @J-Hilk

    I press first Connect button and than after i press subscribed button.

    but it is crash...



  • If temprature is supposed to be temperature, this is a bug:
    mosq->subscribe(NULL, "sensors/temprature", 1);


  • Qt Champions 2018

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



  • @jsulm

    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.



  • @mvuori

    no it is supposed to be sensors/temprature.


  • Qt Champions 2018

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

    qDebug() << mosq;
    

    instead to see the value of the pointer.
    Without stack trace I can't tell you what is going on...



  • @jsulm

    when i execute "qDebug() << mosq;" it gives following output.

    qtmosq(0xac50b0)
    

    how i see stack trace in qt?

    can you please explore me more about stack trace?



  • @Milav

    int size = sizeof(*mosq);

    @jsulm

    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 of mosq is or what it points at run-time, rather this statement just assigns sizeof(qtmosq) regardless. As he says, show us what is in the mosq.



  • @JonB

    when i execute "qDebug() << mosq;" it gives following output.

    qtmosq(0xac50b0)



  • @Milav

    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 likeqDebug() << mosq->something;, where something is some field you know is supposed to exist & be valid in a qtmosq. Or try qDebug() << *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.


  • Qt Champions 2018

    @JonB said in segmentation fault:

    I'm not sure this is actually trying to deference the pointer

    It does not, but I wanted to see its value :-)
    Looks like it's not NULL.



  • @jsulm
    Indeedy :) So now let's see how qDebug() << *mosq fares?



  • @JonB said in segmentation fault:

    qDebug() << *mosq

    gives error :-

    /home/tdp0009/Teq_Projects/Aug_2019/HMI/Firmware/QT/12_oct_hmi/qt-mosquitto-master/mainwindow.cpp:54: note:   'qtmosq' is not derived from 'const QFlags<T>'
    

    @JonB said in segmentation fault:

    qDebug() << mosq->something

    i tried following things and it output are '0'.

    1. qDebug() << mosq->connect_async(host.data(), port);
    2. qDebug() << mosq->loop_start();
    3. qDebug() << mosq->subscribe(NULL, "sensors/temprature", 1);

    the output of all above command are zero (0).

    @JonB said in segmentation fault:

    app from inside a debugge

    i am not able to run the debugger because it gives following error.

    The inferior stopped because it received a signal from the Operating System.
    
    Signal name : 
    SIGILL
    Signal meaning : 
    Illegal instruction
    
    

    hope you got your answer.



  • @JonB said in segmentation fault:

    @jsulm
    Indeedy :) So now let's see how qDebug() << *mosq fares?

    sorry the error is as following not previous one.

    /home/tdp0009/Teq_Projects/Aug_2019/HMI/Firmware/QT/12_oct_hmi/qt-mosquitto-master/mainwindow.cpp:54: error: no match for 'operator<<' in 'qDebug()() << *((MainWindow*)this)->MainWindow::mosq'
    


  • @Milav
    First you wrote

    and i debug the application via qDebug(), the application are crash on following line of code.
    mosq->subscribe(NULL, "sensors/temprature", 1); // Segmentation falut

    Now you write

    qDebug() << mosq->subscribe(NULL, "sensors/temprature", 1);
    the output of all above command are zero (0).

    This is a problem if you say sometimes it seg faults and sometimes it returns 0? Is that the case? You need to be very specific in your answers!

    i am not able to run the debugger because it gives following error.

    That is indeed a big problem. There is little for us to answer from what you have posted. Is a debugger supposed to work for your "Embedded Custom Board" (I don't know about this)? You really need one. Otherwise you are likely to have to try printing out all sorts of information in your code to try to diagnose what is going on....



  • Screenshot from 2019-10-15 13-03-18.png


  • Qt Champions 2018

    @Milav Forget about

    qDebug() << *mosq;
    

    it's only going to work if operator<< was overloaded for that class, apparently it isn't, but this is not an issue.
    Can you try to run the app directly with gdb?



  • @jsulm

    Can you try to run the app directly with gdb?

    I know this is not the place for a long explanation. But when OP says

    I make a qt application for Embedded Custom Board

    I thought (knowing nothing about this) it meant he is doing something like )cross-)compiling of whatever but the app runs "on the board", so he might not have/be able to use a debugger? Can you explain to me in one sentence what the OP's enthronement actually is, I'd like to have the vaguest understanding....



  • @jsulm said in segmentation fault:

    Forget about
    qDebug() << *mosq;

    okay thank you.

    @jsulm said in segmentation fault:

    Can you try to run the app directly with gdb?

    yes i tried but , debugger give me error,

    the error is shown in below image.

    Screenshot from 2019-10-15 13-11-13.png



  • @Milav
    I think @jsulm is asking you try running your app directly in gdb (from the command-line), not from within Qt Creator, as Creator might be the cause of your debugger problem.


  • Moderators

    @Milav ok, since you're able to run the debugger from QtCreator, try the following, select Debugin the menu bar, and select Start and Break on main that should work, and then step through your code line by line.



  • @JonB said in segmentation fault:

    app directly in gdb (from the command-line)

    actually, i am new in this.

    give me some time i explore it and give you answer.

    i don't know how i run app in gdb.



  • @Milav

    1. Open Command Prompt/terminal/shell.
    2. cd to where your executable is.
    3. gdb <your-executable>