Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

QtCreator Debugger evaluates wrong values



  • Hello,

    I have a somewhat strange problem with the Debug mode in QtCreator. I use QtCreator 4.8.1 mingw 4.9.2 32bit and Qt 5.6.3 with the GNU gdb 7.8 for MinGW 4.9.2 32bit.

    The following line of code produces a wrong value, but only if the app is run in debug mode. If I start it outside the QtCreator everything is fine:

    T* temp = reinterpret_cast<T*>(anArray.mid(aStartIndex, size).data());

    T is of type float here an anArray is a QByteArray. The section mid() is taking from the array is 40 A0 00 00, thus it should evaluate to 5 or 5.74869e-41 depending on byte order. But in fact the value I get is -1.58839967e+38 (lowest float number possible).
    On another machine with the same set of Qt, compiler, ... it works fine. Also this problem only occurs if exactly the line above is executed. Changing the code just slightly to:

    T temp = * reinterpret_cast<T*>(anArray.mid(aStartIndex, size).data()); (no longer T*)

    or

    QByteArray array = anArray.mid(aStartIndex, size);
    T* temp = reinterpret_cast<T*>(array.data());

    results in getting the correct value.

    Has anybody an idea why this happens and how to fix it without touching the code?

    Best regards



  • @JealousFish

    Did you already rerun qmake and rebuild all?



  • Yes I did, several times. I even un- and reinstalled Qt and QtCreator.


  • Lifetime Qt Champion

    @JealousFish Can you provide a minimal, compile- and testable example?



  • mainwindow.cpp:

    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    
    template<typename T> T fromByteArray(const QByteArray& anArray, int aStartIndex = 0);
    
    MainWindow::MainWindow(QWidget *parent) :
        QMainWindow(parent),
        ui(new Ui::MainWindow)
    {
        QByteArray test_array = QByteArray::fromHex("8d8d8d8d8d8d8d8d8d8d8d8d8d8d40a000008d8d8d8d8d8d8d8d8d8d8d8d8d8d8d8d8d8d8d8d8d8d8d8d8d8d8d8d8d8d8d8d8d8d8d8d8d8d8d");
        float test_value = fromByteArray<float>(test_array, 14);
        throw std::invalid_argument(std::to_string(test_value));
    }
    
    template<typename T> T fromByteArray(const QByteArray& anArray, int aStartIndex) {
        int size = static_cast<int>(sizeof(T));
        QByteArray test = anArray.mid(aStartIndex, size);
        T* temp = reinterpret_cast<T*>(anArray.mid(aStartIndex, size).data());
        T value = *temp;
        return value;
    }
    
    MainWindow::~MainWindow()
    {
        delete ui;
    }
    

    main.cpp:

    #include "mainwindow.h"
    #include <QApplication>
    
    int main(int argc, char *argv[])
    {
        QApplication a(argc, argv);
        MainWindow w;
        w.show();
    
        return a.exec();
    }
    


  • @JealousFish Hi
    I don’t know why, sometimes when my compiler or debug seems do strange things I must manual remove the compile folder (from explorer file) and rebuild all and then I see no more strange behaviour. Clear All and Rebuild All sometimes seem don’t fix my problem.



  • @CP71
    What folder do you mean by "compile folder"? The folder that is set up during the build in the project? If that's what you mean I've already tried that, otherwise let me know where to find this folder.



  • @JealousFish Yes,
    In Qt Creator is called “Build Directory”, where file .o are created and where you find your application.
    In the past I had the same issue, when I deleted the build folder the problem disappeared.
    Sometimes, when compiler failed and the code seems ok, I delete the builder folder and the problem disappears.
    I think, but it is only my idea, when this happens some files .o are locked, perhaps because I stopped the previous compile.