QTextCursor causes segmentation fault



  • Hello everyone,

    I am developping a motion capture secquence recording program and I am trying to log the color messages in a QTextEdit (black = normal, orange = warning, red = critical).
    Unfortunatly the QTextCursor sometimes causes sefmentation fault.

    Here is my code:

    void MainWindow::logWarning(QString qs_Entry) {
        qWarning() << qs_Entry;
    
        try {
            QTextCursor qtc(qte_Log->textCursor());
    
            QTextCharFormat qtcf;
            qtcf.setForeground(QBrush(QColor("Orange")));
            qtc.setCharFormat(qtcf);
    
            qtc.insertText(qs_Entry + "\n");
        } catch (...) {
            qInfo() << "An unexpected error occured when logging a warning!";
        }
    }
    

    The try catch does not seems to catch the error.

    Did I do something wrong?
    If not, is there another way to display color messages in the QTextEdit without causing segmentation faults?

    Note: I don't know if it's relevant but my program is heavily multithreaded.


  • Lifetime Qt Champion

    Hi,

    Might be a silly question but are you sure that qte_Log is a valid pointer ?

    Otherwise, what does the stack trace tell you ?



  • @bs55 said in QTextCursor causes segmentation fault:

    qte_Log

    Yes, this pointer is valid.

    I forgot to specify that the code works but sometimes causes segmentation fault.

    Here is the debuger stack trace:
    1 ?? 0x7ffff3df5aac
    2 ?? 0x7ffff3dea28e
    3 ?? 0x7ffff3dea2e5
    4 QTextEngine::itemize() const 0x7ffff3dee69c
    5 QTextEngine::lineNumberForTextPosition(int) 0x7ffff3df0bed
    6 QTextLayout::lineForTextPosition(int) const 0x7ffff3df785c
    7 QTextCursorPrivate::setX() 0x7ffff3e47c4e
    8 QTextCursor::insertText(QString const&, QTextCharFormat const&) 0x7ffff3e49775
    9 QTextCursor::insertText(QString const&) 0x7ffff3e4a704
    10 MainWindow::log mainwindow.cpp 843 0x416edb
    11 MainWindow::captureProgressHandler mainwindow.cpp 600 0x414309
    12 QtPrivate::FunctorCall<QtPrivate::IndexesList<0, 1>, QtPrivate::List<int, double>, void, void (MainWindow:: *)(int, double)>::call qobjectdefs_impl.h 136 0x425cf0
    13 QtPrivate::FunctionPointer<void (MainWindow:: *)(int, double)>::call<QtPrivate::List<int, double>, void> qobjectdefs_impl.h 169 0x4251bf
    14 QtPrivate::QSlotObject<void (MainWindow:: *)(int, double), QtPrivate::List<int, double>, void>::impl qobject_impl.h 120 0x4243c6
    15 QMetaObject::activate(QObject *, int, int, void * *) 0x7ffff3756b96
    16 CaptureThread::signalCaptureProgress moc_capturethread.cpp 158 0x42d414
    17 CaptureThread::run capturethread.cpp 98 0x427dc8
    18 ?? 0x7ffff355d70a
    19 start_thread pthread_create.c 333 0x7ffff1b926ba
    20 clone clone.S 109 0x7ffff2c5841d
    ... <More>



  • @bs55

    The try catch does not seems to catch the error.

    Just to be clear, try ... catch will only catch genuine, recoverable "errors". Once you have something like a Seg Fault, nothing will catch that.


  • Moderators

    @bs55 To add to @JonB : Qt itself does not use (throw) exceptions.



  • @bs55 said in QTextCursor causes segmentation fault:

    Note: I don't know if it's relevant but my program is heavily multithreaded.

    Is MainWindow::logWarning called directly from a secondary thread?



  • @jsulm said in QTextCursor causes segmentation fault:

    @bs55 To add to @JonB : Qt itself does not use (throw) exceptions.

    OK. But if the underlying OS/windowing system does throw exceptions, would Qt catch them guaranteed or would they pass through to the calling code?


  • Lifetime Qt Champion

    Qt by default is build without exception support disabled.

    For the rest see the Exception Safety chapter in Qt's documentation.



  • @VRonin No the threads emit signals connected to a slot that uses the logWarning function.


  • Moderators

    @bs55 Did you use QueuedConnection in connect?



  • @bs55 said in QTextCursor causes segmentation fault:

    No the threads emit signals connected to a slot that uses the logWarning function

    Looks like they are connected with Qt::DirectConnection though which is equal to calling the slot from another thread

    11 MainWindow::captureProgressHandler mainwindow.cpp 600 0x414309
    [...]
    16 CaptureThread::signalCaptureProgress moc_capturethread.cpp 158 0x42d414
    17 CaptureThread::run capturethread.cpp 98 0x427dc8
    

Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.