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. Corrupted characters in std::string
QtWS25 Last Chance

Corrupted characters in std::string

Scheduled Pinned Locked Moved Solved General and Desktop
8 Posts 3 Posters 1.1k 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.
  • M Offline
    M Offline
    munderwoods
    wrote on last edited by
    #1

    Hey, I've been researching this issue for a bit not and I just can't resolve it. I'm passing strings into a function and sticking it in a new thread, but when the strings get to the new function the first few characters are almost always corrupted. I'm on ubuntu running qt 5 with g++, and c++ 11. I tried adding CONFIG += c++11 to my .pro file, but same problem. Here is the code:

    How the function is called:

    std::string idToken = firebaseTokens.at("idToken");
    std::thread thread(startStream, std::ref(databaseURL), std::ref(locationId), std::ref(idToken));
    

    How it is defined:

    static void startStream(std::string databaseURL, std::string locationId, std::string idToken)
    {
      std::string fullUrl = std::string(databaseURL + locationId + ".json" + "?" + paramsString(
        {{"auth", idToken}}
      ));
    
      std::string response = httpRequest(fullUrl, "GET", "", "", true);
    }
    

    When I cout idToken outside of the function call it starts like:

    eyJhbGciOiJSUzI1NiIsImtpZCI6IjU4ODI0YTI2ZjFlY2Q1NjEyN2U4OWY1YzkwYTg4MDYxMTJhYmU5OWMiLCJ0eXAiOiJKV1QifQ
    

    Inside of the function call it starts like:

    hUrUNiIsImtpZCI6IjU4ODI0YTI2ZjFlY2Q1NjEyN2U4OWY1YzkwYTg4MDYxMTJhYmU5OWMiLCJ0eXAiOiJKV1QifQ
    

    You can see here that only the first few characters are wrong, and the end string is always a bit shorter.

    Any info on how to proceed would be greatly appreciated, thanks.

    1 Reply Last reply
    0
    • C Offline
      C Offline
      ChrisW67
      wrote on last edited by ChrisW67
      #2

      Nothing to do with Qt. This code is all standard C++ that works fine here (with or without std::ref()):

      #include <iostream>
      #include <string>
      #include <thread>
      #include <chrono>
      
      static void startStream(std::string databaseURL, std::string locationId, std::string idToken)
      {
              std::this_thread::sleep_for(std::chrono::seconds(5));
              std::cout << "startStream()" << std::endl;
              std::cout << databaseURL << std::endl;
              std::cout << locationId << std::endl;
              std::cout << idToken << std::endl;
      }
      
      
      int main()
      {
              std::string databaseURL("http://dummy.local/url");
              std::string locationId("dummyLocationId");
              std::string idToken("eyJhbGciOiJSUzI1NiIsImtpZCI6IjU4ODI0YTI2ZjFlY2Q1NjEyN2U4OWY1YzkwYTg4MDYxMTJhYmU5OWMiLCJ0eXAiOiJKV1QifQ");
      
              std::cout << "main()" << std::endl;
              std::cout << databaseURL << std::endl;
              std::cout << locationId << std::endl;
              std::cout << idToken << std::endl;
              std::thread thread(startStream, std::ref(databaseURL), std::ref(locationId), std::ref(idToken));
      
              thread.join();
              return 0;
      }
      
      $ g++ -o test main.cpp  && ./test
      main()
      http://dummy.local/url
      dummyLocationId
      eyJhbGciOiJSUzI1NiIsImtpZCI6IjU4ODI0YTI2ZjFlY2Q1NjEyN2U4OWY1YzkwYTg4MDYxMTJhYmU5OWMiLCJ0eXAiOiJKV1QifQ
      startStream()
      http://dummy.local/url
      dummyLocationId
      eyJhbGciOiJSUzI1NiIsImtpZCI6IjU4ODI0YTI2ZjFlY2Q1NjEyN2U4OWY1YzkwYTg4MDYxMTJhYmU5OWMiLCJ0eXAiOiJKV1QifQ
      

      Something else about your code is at issue. Start by ensuring a consistent, clean build.

      M 1 Reply Last reply
      0
      • C ChrisW67

        Nothing to do with Qt. This code is all standard C++ that works fine here (with or without std::ref()):

        #include <iostream>
        #include <string>
        #include <thread>
        #include <chrono>
        
        static void startStream(std::string databaseURL, std::string locationId, std::string idToken)
        {
                std::this_thread::sleep_for(std::chrono::seconds(5));
                std::cout << "startStream()" << std::endl;
                std::cout << databaseURL << std::endl;
                std::cout << locationId << std::endl;
                std::cout << idToken << std::endl;
        }
        
        
        int main()
        {
                std::string databaseURL("http://dummy.local/url");
                std::string locationId("dummyLocationId");
                std::string idToken("eyJhbGciOiJSUzI1NiIsImtpZCI6IjU4ODI0YTI2ZjFlY2Q1NjEyN2U4OWY1YzkwYTg4MDYxMTJhYmU5OWMiLCJ0eXAiOiJKV1QifQ");
        
                std::cout << "main()" << std::endl;
                std::cout << databaseURL << std::endl;
                std::cout << locationId << std::endl;
                std::cout << idToken << std::endl;
                std::thread thread(startStream, std::ref(databaseURL), std::ref(locationId), std::ref(idToken));
        
                thread.join();
                return 0;
        }
        
        $ g++ -o test main.cpp  && ./test
        main()
        http://dummy.local/url
        dummyLocationId
        eyJhbGciOiJSUzI1NiIsImtpZCI6IjU4ODI0YTI2ZjFlY2Q1NjEyN2U4OWY1YzkwYTg4MDYxMTJhYmU5OWMiLCJ0eXAiOiJKV1QifQ
        startStream()
        http://dummy.local/url
        dummyLocationId
        eyJhbGciOiJSUzI1NiIsImtpZCI6IjU4ODI0YTI2ZjFlY2Q1NjEyN2U4OWY1YzkwYTg4MDYxMTJhYmU5OWMiLCJ0eXAiOiJKV1QifQ
        

        Something else about your code is at issue. Start by ensuring a consistent, clean build.

        M Offline
        M Offline
        munderwoods
        wrote on last edited by munderwoods
        #3

        @ChrisW67 Welp, thanks. I'm pretty new to c++, so I don't really know where to start with something like this, but I'll backtrack and go at it all again.

        You're sure it's not related to this, though? https://stackoverflow.com/questions/38622444/stdstring-values-become-corrupted-in-qt-5
        I did try the fix and that didn't help so that's a point against it of course.

        The problem only cropped up when I added qt to the project though.

        C 1 Reply Last reply
        0
        • M munderwoods

          @ChrisW67 Welp, thanks. I'm pretty new to c++, so I don't really know where to start with something like this, but I'll backtrack and go at it all again.

          You're sure it's not related to this, though? https://stackoverflow.com/questions/38622444/stdstring-values-become-corrupted-in-qt-5
          I did try the fix and that didn't help so that's a point against it of course.

          The problem only cropped up when I added qt to the project though.

          C Offline
          C Offline
          ChrisW67
          wrote on last edited by
          #4

          @munderwoods Simply referring to the Qt5 libraries should not do anything. Passing the strings through a Qt function might. Can you produce a small program like mine that fails?

          M 1 Reply Last reply
          0
          • C ChrisW67

            @munderwoods Simply referring to the Qt5 libraries should not do anything. Passing the strings through a Qt function might. Can you produce a small program like mine that fails?

            M Offline
            M Offline
            munderwoods
            wrote on last edited by
            #5

            @ChrisW67 Nope, I can not. I'll rebuild from the ground up. Thanks.

            M 1 Reply Last reply
            0
            • M munderwoods

              @ChrisW67 Nope, I can not. I'll rebuild from the ground up. Thanks.

              M Offline
              M Offline
              munderwoods
              wrote on last edited by
              #6

              @munderwoods Looks like it only happens when I detach the thread, it actually works consistently if I join it...

              Christian EhrlicherC 1 Reply Last reply
              0
              • M munderwoods

                @munderwoods Looks like it only happens when I detach the thread, it actually works consistently if I join it...

                Christian EhrlicherC Offline
                Christian EhrlicherC Offline
                Christian Ehrlicher
                Lifetime Qt Champion
                wrote on last edited by
                #7

                @munderwoods said in Corrupted characters in std::string:

                Looks like it only happens when I detach the thread, it actually works consistently if I join it...

                Correct, because idToken is already destructed when the thread runs because you only pass a reference to the thread and idToken is a local variable. At least this is what it looks like from your code pieces. Please provide a minimal, compilable example.

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

                M 1 Reply Last reply
                3
                • Christian EhrlicherC Christian Ehrlicher

                  @munderwoods said in Corrupted characters in std::string:

                  Looks like it only happens when I detach the thread, it actually works consistently if I join it...

                  Correct, because idToken is already destructed when the thread runs because you only pass a reference to the thread and idToken is a local variable. At least this is what it looks like from your code pieces. Please provide a minimal, compilable example.

                  M Offline
                  M Offline
                  munderwoods
                  wrote on last edited by
                  #8

                  @Christian-Ehrlicher You were right, I was declaring my variables locally then they were being destroyed as soon as I detached the thread. It was webdev thinking. Sorry to waste everyone's time!

                  1 Reply Last reply
                  0
                  • SGaistS SGaist has marked this topic as solved on

                  • Login

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