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

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



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



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


  • Lifetime Qt Champion

    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();@



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



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

    Thanks.


  • Moderators

    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)



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


Log in to reply