简体   繁体   English

在 Qt Creator 中使用 CDB 中断断言

[英]Breaking on assertions with CDB in Qt Creator

Every now and then I get an assertion in QVector::operator [] .QVector::operatorQVector::operator [] 中得到一个断言。 I'm unable to break on this assertion.我无法打破这个断言。

I've tried all of the suggestions in this answer, and they either don't work, or are not what I'm after:我已经尝试了这个答案中的所有建议,但它们要么不起作用,要么不是我所追求的:

You can install a handler for the messages/warnings that Qt emits, and do your own processing of them.您可以为 Qt 发出的消息/警告安装一个处理程序,并自行处理它们。

I would rather not have to do this each time it happens.我宁愿不必每次都这样做。

in qt Creator go to Tools -> Options -> Debugger -> GDB and select "Stop when a qFatal is issued"在 qt Creator 中,转到工具 -> 选项 -> 调试器 -> GDB 并选择“发出 qFatal 时停止”

This option doesn't exist for CDB. CDB 不存在此选项。

I have coded a BreakInDebugger function by hand and an assert macro that calls the function.我已经手动编写了一个 BreakInDebugger 函数和一个调用该函数的 assert 宏。

I don't want to have to touch Qt code.我不想接触 Qt 代码。 Besides, I could also just edit qvector.h and add a line that I can actually break on:此外,我还可以编辑qvector.h并添加一行我可以实际中断的行:

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

Is this possible using only Creator?这可以仅使用 Creator 吗?


Update: It seems to be related to where the assertion is triggered from.更新:这似乎与触发断言的位置有关。 An example like this works:这样的例子有效:

#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();
}

That is, I get the typical error dialog with options to debug the assertion:也就是说,我得到了带有调试断言选项的典型错误对话框:

---------------------------
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   
---------------------------

However, with a unit test project, I don't get any dialog:但是,对于单元测试项目,我没有收到任何对话框:

#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"

As discovered in the bug report , this is due to testlib's use of a crash handler that disables the error dialog that usually occurs with assertions.正如在错误报告中发现的那样,这是由于 testlib 使用崩溃处理程序,该处理程序禁用了通常与断言一起出现的错误对话框。 Passing the following application argument to the auto test solves the issue:将以下应用程序参数传递给自动测试可解决此问题:

-nocrashhandler

This change improves the documentation for this feature.更改改进了此功能的文档。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM