T
T
tugo2014-11-18 01:05:53
Programming
tugo, 2014-11-18 01:05:53

When debugging, QtCreator doesn't stop at an error, how to debug then?

In debug mode, the application crashes with the following information:

ASSERT failure in QVector<T>::operator[]: "index out of range", file D:\Qt\Qt5.3.1_mingw\5.3\mingw482_32\include/QtCore/qvector.h, line 385
Invalid parameter passed to C runtime function.

It's great, but useless. Why doesn't QtCreator stop at the error location? What then is the use of this assertion? How then to find a place with an error?
Maybe you need to somehow configure the debugger?
UPDATE
Thanks for the help.
The question came down to where to put the breakpoint.
No breakpoint was set in the qvector.h file:
template <typename T>
inline T &QVector<T>::operator[](int i)
{ Q_ASSERT_X(i >= 0 && i < d->size, "QVector<T>::operator[]", "index out of range");
  return data()[i]; }

Got out of the situation by overriding QtMessageHandler:
#include <iostream>
#include <QApplication>
#include <QString>

void myMessageOutput(QtMsgType type, const QMessageLogContext & context, const QString & msg)
{
    switch (type)
    {
    case QtDebugMsg:
        std::cerr << QString("Debug: %1 (%2:%3, %4)\n").arg(msg).arg(context.file).arg(context.line).arg(context.function).toStdString();
        break;
    case QtWarningMsg:
        std::cerr << QString("Warning: %1 (%2:%3, %4)\n").arg(msg).arg(context.file).arg(context.line).arg(context.function).toStdString();
        break;
    case QtCriticalMsg:
        std::cerr << QString("Critical: %1 (%2:%3, %4)\n").arg(msg).arg(context.file).arg(context.line).arg(context.function).toStdString();
        break;
    case QtFatalMsg:
        std::cerr << QString("Fatal: %1 (%2:%3, %4)\n").arg(msg).arg(context.file).arg(context.line).arg(context.function).toStdString();
        abort();
    }
}

int main(int argc, char *argv[])
{
    qInstallMessageHandler(myMessageOutput);
    QApplication a(argc, argv);
    
    return a.exec();
}

on abort(); a breakpoint has been set.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
X
xandox, 2014-11-18
@tugo

He shouldn't. Put a breakpoint where you need it and it will stop there.
asserts are needed in order to inform the developer that he screwed up. Internally, assert calls abort() which unconditionally aborts program execution.

M
mamkaololosha, 2014-11-18
@mamkaololosha

> "index out of range"
He wrote everything. The assert is called inside the vector. Where should he stay? In a vector? Set breakpoints and see what happens. Screw gdb qt-project.org/doc/qtcreator-2.6/creator-debugger-...

T
TriKrista, 2014-11-18
@TriKrista

Here, in the course, it all depends on the OS.
On Linux, QtCreator stops at the location of the error.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question