Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Differing QString.toStdString() behavior between Debug and Release QTCore.dll
QtWS25 Last Chance

Differing QString.toStdString() behavior between Debug and Release QTCore.dll

Scheduled Pinned Locked Moved General and Desktop
8 Posts 4 Posters 3.0k 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
    Deleterious
    wrote on last edited by
    #1

    Compiler: Visual Studio 2008
    Version: 5.2.1
    Configure command: configure -mp -debug-and-release -opensource -shared -platform win32-msvc2008 -nomake tests -nomake examples -skip qtwebkit -skip qtwebkit-examples -skip qtandroidextras -skip qtmacextras -skip qtlocation -skip qtx11extras -skip qtsensors -skip qtconnectivity -skip qtgraphicaleffects -skip qtimageformats -skip qtlocation -skip qtserialport -skip qtsvg -skip qtmultimedia -skip qtxmlpatterns -opengl desktop

    When I utilize the debug QTCored.lib and .dll, QString behavior is as I would expect:

    @char* foo = "test";
    QString bar(foo);
    std::string baz = bar.toStdString();

    QByteArray foozle = bar.toUtf8()
    std::string basil = foozle.data();@

    In the Visual studio 2012 debugger (yes, I'm debugging in a later version than I'm compiling in.. long story), bar = "test", baz = "test" and basil = "test".

    When I utilize the release QTCore.lib and .dll, however, the behavior changes. bar = "test", baz = <Error reading characters of String> and basil = "test".

    We started development with a developer-build version of the library and much of the code breaks when we created a build linking the release library instead of our original developer-build one. To make certain we were comparing apples to apples I rebuilt the libraries with the configure string above from the 5.2.1 source. I can recreate the behavior simply by replacing one .lib/.dll with the other (obviously with a rename).

    Any thoughts?

    1 Reply Last reply
    0
    • D Offline
      D Offline
      Deleterious
      wrote on last edited by
      #2

      I wanted to clarify that all of the modules we've written are being compiled with the Visual Studio 2008 compiler as well - we're not trying to do any cross-compiler shenanigans. We're simply using the Visual Studio 2012 GUI for our development.

      1 Reply Last reply
      0
      • SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on last edited by
        #3

        Hi and welcome to devnet,

        Here is your problem:

        @std::string basil = bar.toUtf8().data();@

        You are assigning to basil a char * pointer to the content of a temporary QByteArray.

        If you really want to do something like that:

        @
        QByteArray baz = bar.toUtf8();
        std::string basil = baz.data();@

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        1 Reply Last reply
        0
        • D Offline
          D Offline
          Deleterious
          wrote on last edited by
          #4

          Thanks for your reply and the welcome, SGaist.

          I get your point about the temporary array, but the basil variable was included simply to show that not all of the ways to get the data out of the QString were failing. In this case the problem variable is baz which alternates between "test" (as expected) and line noise depending on which variant of the lib is linked.

          I've edited the original code sample to make this more clear.

          1 Reply Last reply
          0
          • D Offline
            D Offline
            Deleterious
            wrote on last edited by
            #5

            Bumping discussion as I still don't have a way to resolve this. Anyone have any further thoughts?

            Thanks.

            1 Reply Last reply
            0
            • JKSHJ Offline
              JKSHJ Offline
              JKSH
              Moderators
              wrote on last edited by
              #6

              Hi,

              How do you compile/link your program?

              Are you able to retry the test with a newer compiler?

              [quote]We started development with a developer-build version of the library and much of the code breaks when we created a build linking the release library instead of our original developer-build one.[/quote]How does it break?

              [quote]I can recreate the behavior simply by replacing one .lib/.dll with the other (obviously with a rename).[/quote]Did you also swap all the other DLLs that your program needs? They must be either all-debug or all-release; you cannot mix them. (But FYI, the correct way to do it is link to Qt5Cored.dll for the debug build and link to Qt5Core.dll for the release build)

              Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

              1 Reply Last reply
              0
              • hskoglundH Offline
                hskoglundH Offline
                hskoglund
                wrote on last edited by
                #7

                Hi, maybe this is just a case of the release library freeing string data faster than the debug flavor. Just a guess, but try swapping line 3 and line 5 with each other, so that baz gets the last shot...

                1 Reply Last reply
                0
                • D Offline
                  D Offline
                  Deleterious
                  wrote on last edited by
                  #8

                  [quote author="JKSH" date="1398705169"]Hi,

                  Are you able to retry the test with a newer compiler?
                  [/quote]
                  Sadly, no, such is not my luck.

                  [quote author="JKSH" date="1398705169"]
                  [quote]We started development with a developer-build version of the library and much of the code breaks when we created a build linking the release library instead of our original developer-build one.[/quote]How does it break?
                  [/quote]
                  We were seeing the variable corruption everywhere we used the .toStdString() call.

                  [quote author="JKSH" date="1398705169"]
                  [quote]I can recreate the behavior simply by replacing one .lib/.dll with the other (obviously with a rename).[/quote]Did you also swap all the other DLLs that your program needs? They must either be all-debug or all-release; you cannot mix them. (But FYI, the correct way to do it is link to Qt5Cored.dll for the debug build and link to Qt5Core.dll for the release build)[/quote]
                  [/quote]

                  That's a.. really good point. I spent some time yesterday going hardline on the project files and enforcing the Debug/Release line. So far it seems like this effort may actually have worked - the initial points where we were seeing the issue are behaving again. More in-depth testing is needed but this at least gives us a path forward for a while. Thanks!

                  [quote author="hskoglund" date="1398705169"]
                  Hi, maybe this is just a case of the release library freeing string data faster than the debug flavor. Just a guess, but try swapping line 3 and line 5 with each other, so that baz gets the last shot…
                  [/quote]

                  I'll give this a try if our library shuffle doesn't end up doing the job. Thanks for the reply.

                  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