Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt Creator and other tools
  4. QtCreator Debugger evaluates wrong values
QtWS25 Last Chance

QtCreator Debugger evaluates wrong values

Scheduled Pinned Locked Moved Unsolved Qt Creator and other tools
debugqbytearrayreinterpretcas
8 Posts 4 Posters 1.7k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    JealousFish
    wrote on last edited by JealousFish
    #1

    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

    K CP71C 2 Replies Last reply
    0
    • J JealousFish

      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

      K Offline
      K Offline
      koahnig
      wrote on last edited by
      #2

      @JealousFish

      Did you already rerun qmake and rebuild all?

      Vote the answer(s) that helped you to solve your issue(s)

      1 Reply Last reply
      0
      • J Offline
        J Offline
        JealousFish
        wrote on last edited by
        #3

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

        aha_1980A 1 Reply Last reply
        0
        • J JealousFish

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

          aha_1980A Offline
          aha_1980A Offline
          aha_1980
          Lifetime Qt Champion
          wrote on last edited by
          #4

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

          Qt has to stay free or it will die.

          1 Reply Last reply
          0
          • J Offline
            J Offline
            JealousFish
            wrote on last edited by
            #5

            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();
            }
            
            1 Reply Last reply
            0
            • J JealousFish

              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

              CP71C Offline
              CP71C Offline
              CP71
              wrote on last edited by
              #6

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

              J 1 Reply Last reply
              1
              • CP71C CP71

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

                J Offline
                J Offline
                JealousFish
                wrote on last edited by
                #7

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

                CP71C 1 Reply Last reply
                0
                • J JealousFish

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

                  CP71C Offline
                  CP71C Offline
                  CP71
                  wrote on last edited by
                  #8

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

                  1 Reply Last reply
                  0

                  • Login

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • Users
                  • Groups
                  • Search
                  • Get Qt Extensions
                  • Unsolved