Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. General talk
  3. Qt 6
  4. What changed with QString::toStdString() in QT6?
QtWS25 Last Chance

What changed with QString::toStdString() in QT6?

Scheduled Pinned Locked Moved Solved Qt 6
24 Posts 5 Posters 5.2k 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.
  • D Offline
    D Offline
    Dmitriano
    wrote on 23 Sept 2021, 10:50 last edited by Dmitriano
    #1

    The program below crashes with MSVC2019 in Debug configuration:

    int main(int argc, char *argv[])
    {
        const QString qstr = "QT6";
        const std::string val = qstr.toStdString();
        std::cout << val; //comment this out to make the program work.
        qDebug() << QString::fromStdString(val);
        return 0;
    }
    

    An exception occur in MSVC sources:
    alt text

    but if I comment std::cout << val; out it works.

    I compile the program with /std:c++latest

    Built QT with MSVC 2019 from branch v6.2.0-rc1 with the following configuration

    CALL "C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Auxiliary\Build\vcvarsall.bat" amd64
    ...
    configure.bat -prefix %MY_INSTALL_PATH% -DQT_NO_EXCEPTIONS=1 -release -force-debug-info -platform win32-msvc -opengl es2 -opensource -confirm-license
    -I "C:\Program Files\OpenSSL\include" -L "C:\Program Files\OpenSSL\lib"
    

    See my blog post for more information on how I built QT6.

    EDIT1:

    Created a minimal example (main.cpp + CMakeLists.txt)

    compile it with:

    cmake.exe ..\..\StringTest -G "Visual Studio 16 2019" -A x64 -DCMAKE_PREFIX_PATH=E:\Qt\Qt6.2\windows
    
    J 1 Reply Last reply 23 Sept 2021, 11:24
    0
    • C Online
      C Online
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on 23 Sept 2021, 11:15 last edited by
      #2

      Doesn't look like Qt can do anything against this - likely more a compiler bug or your env is messed up.
      Can you print a std::string which is not created from a QString to std::cout?

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      D 2 Replies Last reply 23 Sept 2021, 11:24
      1
      • D Dmitriano
        23 Sept 2021, 10:50

        The program below crashes with MSVC2019 in Debug configuration:

        int main(int argc, char *argv[])
        {
            const QString qstr = "QT6";
            const std::string val = qstr.toStdString();
            std::cout << val; //comment this out to make the program work.
            qDebug() << QString::fromStdString(val);
            return 0;
        }
        

        An exception occur in MSVC sources:
        alt text

        but if I comment std::cout << val; out it works.

        I compile the program with /std:c++latest

        Built QT with MSVC 2019 from branch v6.2.0-rc1 with the following configuration

        CALL "C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Auxiliary\Build\vcvarsall.bat" amd64
        ...
        configure.bat -prefix %MY_INSTALL_PATH% -DQT_NO_EXCEPTIONS=1 -release -force-debug-info -platform win32-msvc -opengl es2 -opensource -confirm-license
        -I "C:\Program Files\OpenSSL\include" -L "C:\Program Files\OpenSSL\lib"
        

        See my blog post for more information on how I built QT6.

        EDIT1:

        Created a minimal example (main.cpp + CMakeLists.txt)

        compile it with:

        cmake.exe ..\..\StringTest -G "Visual Studio 16 2019" -A x64 -DCMAKE_PREFIX_PATH=E:\Qt\Qt6.2\windows
        
        J Online
        J Online
        JonB
        wrote on 23 Sept 2021, 11:24 last edited by JonB
        #3

        @Dmitriano
        Further to @Christian-Ehrlicher . I would suggest you Google for qstring tostdstring crash and read through the various problems/solutions (particularly stackoverflow). I don't know whether it applies to you, and I don't know about Qt 6, but check you are not falling foul of what they say about being careful what libraries you are linking with?

        D 1 Reply Last reply 23 Sept 2021, 11:28
        0
        • C Christian Ehrlicher
          23 Sept 2021, 11:15

          Doesn't look like Qt can do anything against this - likely more a compiler bug or your env is messed up.
          Can you print a std::string which is not created from a QString to std::cout?

          D Offline
          D Offline
          Dmitriano
          wrote on 23 Sept 2021, 11:24 last edited by
          #4

          @Christian-Ehrlicher said in What changed with QString::toStdString() in QT6?:

          Can you print a std::string which is not created from a QString to std::cout?

          it is what I am trying to do.

          J C 2 Replies Last reply 23 Sept 2021, 11:26
          0
          • D Dmitriano
            23 Sept 2021, 11:24

            @Christian-Ehrlicher said in What changed with QString::toStdString() in QT6?:

            Can you print a std::string which is not created from a QString to std::cout?

            it is what I am trying to do.

            J Online
            J Online
            JonB
            wrote on 23 Sept 2021, 11:26 last edited by
            #5

            @Dmitriano
            No, @Christian-Ehrlicher is suggesting you start by removing all your Qt stuff here and just checking how it behaves with nothing but std::string, which you have not reported so far.

            1 Reply Last reply
            0
            • J JonB
              23 Sept 2021, 11:24

              @Dmitriano
              Further to @Christian-Ehrlicher . I would suggest you Google for qstring tostdstring crash and read through the various problems/solutions (particularly stackoverflow). I don't know whether it applies to you, and I don't know about Qt 6, but check you are not falling foul of what they say about being careful what libraries you are linking with?

              D Offline
              D Offline
              Dmitriano
              wrote on 23 Sept 2021, 11:28 last edited by
              #6

              @JonB said in What changed with QString::toStdString() in QT6?:

              what libraries you are linking with?

              I do not link libraries except OpenSSL 1.1.1g and I added SQLite sources into my project.

              J 1 Reply Last reply 23 Sept 2021, 11:38
              0
              • D Dmitriano
                23 Sept 2021, 11:28

                @JonB said in What changed with QString::toStdString() in QT6?:

                what libraries you are linking with?

                I do not link libraries except OpenSSL 1.1.1g and I added SQLite sources into my project.

                J Online
                J Online
                JonB
                wrote on 23 Sept 2021, 11:38 last edited by JonB
                #7

                @Dmitriano said in What changed with QString::toStdString() in QT6?:

                I do not link libraries except

                Where do you think the std libraries and the run-time I/O libraries etc. come from? You're not doing a completely static build, are you?

                In any case, have you tried without any Qt anything, just to see what the behaviour is?

                D 1 Reply Last reply 23 Sept 2021, 12:16
                1
                • D Dmitriano
                  23 Sept 2021, 11:24

                  @Christian-Ehrlicher said in What changed with QString::toStdString() in QT6?:

                  Can you print a std::string which is not created from a QString to std::cout?

                  it is what I am trying to do.

                  C Online
                  C Online
                  Christian Ehrlicher
                  Lifetime Qt Champion
                  wrote on 23 Sept 2021, 11:43 last edited by
                  #8

                  @Dmitriano said in What changed with QString::toStdString() in QT6?:

                  it is what I am trying to do.

                  Not in your example code. There you create the std::string from a QString instead a plain const char * as I suggested.

                  Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                  Visit the Qt Academy at https://academy.qt.io/catalog

                  D 1 Reply Last reply 23 Sept 2021, 12:05
                  1
                  • C Christian Ehrlicher
                    23 Sept 2021, 11:15

                    Doesn't look like Qt can do anything against this - likely more a compiler bug or your env is messed up.
                    Can you print a std::string which is not created from a QString to std::cout?

                    D Offline
                    D Offline
                    Dmitriano
                    wrote on 23 Sept 2021, 12:00 last edited by
                    #9
                    This post is deleted!
                    1 Reply Last reply
                    0
                    • C Christian Ehrlicher
                      23 Sept 2021, 11:43

                      @Dmitriano said in What changed with QString::toStdString() in QT6?:

                      it is what I am trying to do.

                      Not in your example code. There you create the std::string from a QString instead a plain const char * as I suggested.

                      D Offline
                      D Offline
                      Dmitriano
                      wrote on 23 Sept 2021, 12:05 last edited by
                      #10

                      @Christian-Ehrlicher said in What changed with QString::toStdString() in QT6?:

                      Not in your example code. There you create the std::string from a QString instead a plain const char * as I suggested.

                      The program below works:

                      int main(int argc, char *argv[])
                      {
                          const std::string val = "QT6";
                          std::cout << val << std::endl;
                          qDebug() << QString::fromStdString(val);
                      
                          return 0;
                      }
                      

                      but prints something strange:

                      QT6
                      "\u0010???\u001C\u0002"
                      

                      also see EDIT1 with the link to my minimal example.

                      1 Reply Last reply
                      0
                      • J JonB
                        23 Sept 2021, 11:38

                        @Dmitriano said in What changed with QString::toStdString() in QT6?:

                        I do not link libraries except

                        Where do you think the std libraries and the run-time I/O libraries etc. come from? You're not doing a completely static build, are you?

                        In any case, have you tried without any Qt anything, just to see what the behaviour is?

                        D Offline
                        D Offline
                        Dmitriano
                        wrote on 23 Sept 2021, 12:16 last edited by
                        #11

                        @JonB said in What changed with QString::toStdString() in QT6?:

                        In any case, have you tried without any Qt anything, just to see what the behaviour is?

                        To avoid starting a battle here I created a minimal example, see EDIT1.

                        1 Reply Last reply
                        0
                        • C Online
                          C Online
                          Christian Ehrlicher
                          Lifetime Qt Champion
                          wrote on 23 Sept 2021, 12:31 last edited by Christian Ehrlicher
                          #12

                          I still think this has nothing to do with Qt. QString::toStdString() (and the called QByteArray::toStdString()) are fully inlined and therefore it does not matter how Qt is compiled at all.

                          Try out the following (which is what QString::toStdString() is doing)

                          QString s = "Qt6";
                          QByteArray ba = s.toUtf8();
                          std::string str1 = ba.data();
                          std::string str2 = std::string(ba.data(), ba.size());  // this is what QString::toStdString()  is doing
                          std::cout << qPrintable(s) << std::endl;
                          std::cout << ba.data() << std::endl;
                          std::cout << str1 << std::endl;
                          std::cout << str2 << std::endl;
                          

                          And try to compile with and without /std:c++latest

                          Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                          Visit the Qt Academy at https://academy.qt.io/catalog

                          D 1 Reply Last reply 23 Sept 2021, 12:55
                          1
                          • C Christian Ehrlicher
                            23 Sept 2021, 12:31

                            I still think this has nothing to do with Qt. QString::toStdString() (and the called QByteArray::toStdString()) are fully inlined and therefore it does not matter how Qt is compiled at all.

                            Try out the following (which is what QString::toStdString() is doing)

                            QString s = "Qt6";
                            QByteArray ba = s.toUtf8();
                            std::string str1 = ba.data();
                            std::string str2 = std::string(ba.data(), ba.size());  // this is what QString::toStdString()  is doing
                            std::cout << qPrintable(s) << std::endl;
                            std::cout << ba.data() << std::endl;
                            std::cout << str1 << std::endl;
                            std::cout << str2 << std::endl;
                            

                            And try to compile with and without /std:c++latest

                            D Offline
                            D Offline
                            Dmitriano
                            wrote on 23 Sept 2021, 12:55 last edited by
                            #13

                            @Christian-Ehrlicher with and without /std:c++latest: your code works, but my original example does not.

                            1 Reply Last reply
                            0
                            • C Online
                              C Online
                              Christian Ehrlicher
                              Lifetime Qt Champion
                              wrote on 23 Sept 2021, 12:57 last edited by
                              #14

                              The only difference is the const QString <-> QString now.
                              Ste through your debugger with F11 to see that only those two functions are called.

                              Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                              Visit the Qt Academy at https://academy.qt.io/catalog

                              D 1 Reply Last reply 23 Sept 2021, 13:07
                              0
                              • C Christian Ehrlicher
                                23 Sept 2021, 12:57

                                The only difference is the const QString <-> QString now.
                                Ste through your debugger with F11 to see that only those two functions are called.

                                D Offline
                                D Offline
                                Dmitriano
                                wrote on 23 Sept 2021, 13:07 last edited by Dmitriano
                                #15

                                @Christian-Ehrlicher Noticed that my original example works in Release configuration, but does not work in Debug. So probably the difference is that some copying or moving are optimized. Or probably something goes wrong if I link a debug version of the app with a release version of QT.

                                C 1 Reply Last reply 23 Sept 2021, 14:20
                                0
                                • D Dmitriano
                                  23 Sept 2021, 13:07

                                  @Christian-Ehrlicher Noticed that my original example works in Release configuration, but does not work in Debug. So probably the difference is that some copying or moving are optimized. Or probably something goes wrong if I link a debug version of the app with a release version of QT.

                                  C Online
                                  C Online
                                  Christian Ehrlicher
                                  Lifetime Qt Champion
                                  wrote on 23 Sept 2021, 14:20 last edited by
                                  #16

                                  @Dmitriano said in What changed with QString::toStdString() in QT6?:

                                  if I link a debug version of the app with a release version of QT.

                                  You must not mix debug and release libraries on windows since they use a different MSVCRT

                                  Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                                  Visit the Qt Academy at https://academy.qt.io/catalog

                                  J 1 Reply Last reply 23 Sept 2021, 14:51
                                  3
                                  • C Christian Ehrlicher
                                    23 Sept 2021, 14:20

                                    @Dmitriano said in What changed with QString::toStdString() in QT6?:

                                    if I link a debug version of the app with a release version of QT.

                                    You must not mix debug and release libraries on windows since they use a different MSVCRT

                                    J Online
                                    J Online
                                    JonB
                                    wrote on 23 Sept 2021, 14:51 last edited by JonB
                                    #17

                                    @Christian-Ehrlicher said in What changed with QString::toStdString() in QT6?:

                                    You must not mix debug and release libraries on windows since they use a different MSVCRT

                                    I suggested a library mismatch might be at issue earlier, but seemed to be told this was causing "To avoid starting a battle"....

                                    I assume the OP means he has two quite separate builds of everything, with different, consistent compilation/linkage flags used throughout, and is not mixing anything debug with release at all.....

                                    1 Reply Last reply
                                    1
                                    • J Online
                                      J Online
                                      JonB
                                      wrote on 23 Sept 2021, 15:03 last edited by
                                      #18

                                      @Dmitriano
                                      When I said earlier about Googling qstring tostdstring crash and the stackoverflow post, there is https://stackoverflow.com/a/15611383/489865

                                      Your Qt DLLs need to be compiled with STL support and exactly the same C-Runtime Library as your code. It looks as though you are using two different CRTs at the same time, which would destroy the objects created on one heap by Qt into the heap used by your program.

                                      Check the DLL Usage with the Dependency Walker!

                                      That is what I had in mind, about the "same C-Runtime Library".

                                      Do you have the Windows Dependency Walker? It's a useful tool! If you run it on the misbehaving executable, check carefully for all the dependent libraries. If you see both debug and non-debug versions of things like MSVCRT involved, you have a problem! Everything, including your Qt libraries, must be compiled either for release or for debug, no mixture. Might that be the situation you are in?

                                      D 1 Reply Last reply 23 Sept 2021, 23:38
                                      1
                                      • J JonB
                                        23 Sept 2021, 15:03

                                        @Dmitriano
                                        When I said earlier about Googling qstring tostdstring crash and the stackoverflow post, there is https://stackoverflow.com/a/15611383/489865

                                        Your Qt DLLs need to be compiled with STL support and exactly the same C-Runtime Library as your code. It looks as though you are using two different CRTs at the same time, which would destroy the objects created on one heap by Qt into the heap used by your program.

                                        Check the DLL Usage with the Dependency Walker!

                                        That is what I had in mind, about the "same C-Runtime Library".

                                        Do you have the Windows Dependency Walker? It's a useful tool! If you run it on the misbehaving executable, check carefully for all the dependent libraries. If you see both debug and non-debug versions of things like MSVCRT involved, you have a problem! Everything, including your Qt libraries, must be compiled either for release or for debug, no mixture. Might that be the situation you are in?

                                        D Offline
                                        D Offline
                                        Dmitriano
                                        wrote on 23 Sept 2021, 23:38 last edited by
                                        #19

                                        @JonB said in What changed with QString::toStdString() in QT6?:

                                        Everything, including your Qt libraries, must be compiled either for release or for debug, no mixture. Might that be the situation you are in?

                                        Yes, it is exactly the situation I was in. And I was in the same situation with QT5, but it did not crash.

                                        J 1 Reply Last reply 24 Sept 2021, 05:35
                                        0
                                        • D Dmitriano
                                          23 Sept 2021, 23:38

                                          @JonB said in What changed with QString::toStdString() in QT6?:

                                          Everything, including your Qt libraries, must be compiled either for release or for debug, no mixture. Might that be the situation you are in?

                                          Yes, it is exactly the situation I was in. And I was in the same situation with QT5, but it did not crash.

                                          J Online
                                          J Online
                                          JonB
                                          wrote on 24 Sept 2021, 05:35 last edited by
                                          #20

                                          @Dmitriano said in What changed with QString::toStdString() in QT6?:

                                          And I was in the same situation with QT5, but it did not crash.

                                          Point taken, but that's a bit hit-or-miss. But since you have closed this thread I guess you are saying that sticking to release- or debug-only for everything has resolved your problem?

                                          D 1 Reply Last reply 25 Sept 2021, 16:31
                                          0

                                          6/24

                                          23 Sept 2021, 11:28

                                          topic:navigator.unread, 18
                                          • Login

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