在Qt Creator中打破CDB的断言

Breaking on assertions with CDB in Qt Creator

本文关键字:CDB 断言 Qt Creator      更新时间:2023-10-16

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

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

您可以为Qt发出的消息/警告安装处理程序,并自行处理它们。

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

在 qt 中创建器中转到工具 -> 选项 -> 调试器 -> GDB,然后选择"发出 qFatal 时停止"

国开行不存在此选项。

我已经手动编写了一个 BreakInDebugger 函数和一个调用该函数的断言宏。

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

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

仅使用造物主可以做到这一点吗?


更新:它似乎与断言的触发位置有关。像这样的例子有效:

#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:devqt5-dev-debugqtbaselibQt5Cored.dll
Module: 5.8.0
File: C:devqt5-devqtbasesrccorelibglobalqglobal.cpp
Line: 3045
ASSERT failure in QVector<T>::operator[]: "index out of range", file c:devqt5-dev-debugqtbaseincludeqtcore../../../../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"

正如错误报告中发现的那样,这是由于 testlib 使用了崩溃处理程序,该处理程序禁用了通常与断言一起发生的错误对话框。将以下应用程序参数传递给自动测试可解决此问题:

-nocrashhandler

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