在 Qt Creator 中使用 CDB 中断断言

Breaking on assertions with CDB in Qt Creator

我时不时地在 QVector::operator[] 中得到一个断言。我无法打破这个断言。

我已经尝试了 this 答案中的所有建议,但它们要么不起作用,要么不是我想要的:

You can install a handler for the messages/warnings that Qt emits, and do your own processing of them.

我宁愿不必每次都这样做。

in qt Creator go to Tools -> Options -> Debugger -> GDB and select "Stop when a qFatal is issued"

CDB 不存在此选项。

I have coded a BreakInDebugger function by hand and an assert macro that calls the function.

我不想接触 Qt 代码。此外,我还可以只编辑 qvector.h 并添加一行我可以实际中断的行:

if (i >= 0 && i < d->size)
    qDebug() << "oops";

仅使用 Creator 有可能吗?


更新:好像跟断言从哪里触发有关。像这样的例子有效:

#include <QGuiApplication>
#include <QtQuick>

int main(int argc, char *argv[])
{
    QGuiApplication app(argc, argv);

    QVector<int> i;
    i[0] = 0;

    QQmlApplicationEngine engine;
    engine.load(QUrl(QStringLiteral("qrc:/main.qml")));

    return app.exec();
}

也就是说,我得到了带有调试断言选项的典型错误对话框:

---------------------------
Microsoft Visual C++ Runtime Library
---------------------------
Debug Error!

Program: C:\dev\qt5-dev-debug\qtbase\lib\Qt5Cored.dll
Module: 5.8.0
File: C:\dev\qt5-dev\qtbase\src\corelib\global\qglobal.cpp
Line: 3045

ASSERT failure in QVector<T>::operator[]: "index out of range", file c:\dev\qt5-dev-debug\qtbase\include\qtcore\../../../../qt5-dev/qtbase/src/corelib/tools/qvector.h, line 433

(Press Retry to debug the application)

---------------------------
Abort   Retry   Ignore   
---------------------------

但是,对于单元测试项目,我没有得到任何对话框:

#include <QString>
#include <QtTest>

class Untitled3Test : public QObject
{
    Q_OBJECT

public:
    Untitled3Test();

private Q_SLOTS:
    void testCase1();
};

Untitled3Test::Untitled3Test()
{
}

void Untitled3Test::testCase1()
{
    QVector<int> i;
    i[0] = 0;
}

QTEST_APPLESS_MAIN(Untitled3Test)

#include "tst_untitled3test.moc"

正如在 bug report, this is due to testlib's use of a crash handler 中发现的那样,它禁用了通常与断言一起出现的错误对话框。将以下应用程序参数传递给自动测试可解决问题:

-nocrashhandler

This 更改改进了此功能的文档。