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

Program runs normally but gets Segmentation Fault in debugger



  • I have a simple console app that looks for ports and checks their status for writing.

    #include <QCoreApplication>
    #include <QSerialPort>
    #include <QSerialPortInfo>
    #include <QIODevice>
    #include <iostream>
    
    int main(int argc, char *argv[])
    {
        QCoreApplication a(argc, argv);
    
        QSerialPort port;
        QList<QSerialPortInfo> qports = QSerialPortInfo::availablePorts();
    
        std::cout << qports.length() << " serial ports found\n" << std::endl;
    
        for(const QSerialPortInfo &portinfo : qports) {
            std::cout << "Checking port " << portinfo.portName().toStdString() << std::endl;
    
            port.setPortName(portinfo.portName());
    
    
            port.setBaudRate(QSerialPort::Baud9600);
            port.setDataBits(QSerialPort::Data8);
            port.setParity(QSerialPort::NoParity);
            port.setStopBits(QSerialPort::OneStop);
            port.setFlowControl(QSerialPort::HardwareControl);
            bool isOpen = port.open(QIODevice::WriteOnly);
            std::cout << "Port opened: " << ((isOpen) ? "TRUE" : "FALSE") << std::endl;
            std::cout << "Port writable: " << ((port.isWritable()) ? "TRUE" : "FALSE") << std::endl;
    
            std::cout << "Port is " << ((isOpen && port.isWritable()) ? "" : "not ") << "ready\n" << std::endl;
    
            port.close();
        }
        return a.exec();
    }
    

    The program runs successfully on Windows 10 and my output screen looks like this:

    3 serial ports found
    
    Checking port COM2
    Port opened: FALSE
    Port writable: FALSE
    Port is not ready
    
    Checking port COM3
    Port opened: TRUE
    Port writable: TRUE
    Port is ready
    
    Checking port COM4
    Port opened: FALSE
    Port writable: FALSE
    Port is not ready
    

    I set a breakpoint and start debugger I enter the debugger and expand the port variable to see its members. Then expanding some of those variables (d and properties) causes a Segmentation Fault dialog window to pop up. After I cancel the dialog I see that the issue occured in qserialport_p.h at the start ofthe QSerialPortPrivate class definition.

    class QSerialPortPrivate : public QIODevicePrivate
    {
        Q_DECLARE_PUBLIC(QSerialPort)
    

    I realize that these members have bad addresses. But is this normal or a problem? After I dispose of the dialog the app can continue fine. I want to know if this is a budding problem or if it is just a quirk of the Debug system. I declare my QSerialPort as a variable rather than construct it since I don't have a parent QObject for it. I want to know if I can ignore this and simply not debug into these QSerialPort members or if this is a future issue.


  • Lifetime Qt Champion

    but gets Segmentation Fault

    So where does it crash?

    I realize that these members have bad addresses.

    Qt is compiled in release mode - the debug information can not be accurate then.



  • @Christian-Ehrlicher The program is compiled in Debug mode and runs fine if I don't try to debug it. However when I set a breakpoint (before executing port.close) and go into the debugger. I cannot expand some of the memebers of the port variable without getting a segmentation fault dialog.


  • Lifetime Qt Champion

    @Mike_W said in Program runs normally but gets Segmentation Fault in debugger:

    I cannot expand some of the memebers of the port variable without getting a segmentation fault dialog.

    So not your program crashes but the debugger? What compiler and debugger do you use?



  • @Christian-Ehrlicher I am using MinGW 7.3.0 32-bit for C and C++. They are installed at C:\Qt5.12.6\Tools\mingw730_32\bin on my PC. They were installed with Qt Creator and Qt. The debugger is GNU gdb 8.1 for MinGW 7.3.0 32-bit in the same directory.


  • Lifetime Qt Champion

    What version of QtCreator? Does creator or the debugger crash?


Log in to reply