Corrupted characters in std::string
-
wrote on 13 Mar 2023, 00:04 last edited by
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.
-
wrote on 13 Mar 2023, 01:08 last edited by 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.
-
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.
wrote on 13 Mar 2023, 02:25 last edited by 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.
-
@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.
wrote on 13 Mar 2023, 03:45 last edited by@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?
-
@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?
wrote on 13 Mar 2023, 16:19 last edited by@ChrisW67 Nope, I can not. I'll rebuild from the ground up. Thanks.
-
@ChrisW67 Nope, I can not. I'll rebuild from the ground up. Thanks.
wrote on 13 Mar 2023, 16:48 last edited by@munderwoods Looks like it only happens when I detach the thread, it actually works consistently if I join it...
-
@munderwoods Looks like it only happens when I detach the thread, it actually works consistently if I join it...
@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.
-
@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.
wrote on 13 Mar 2023, 18:06 last edited by@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!
-
S SGaist has marked this topic as solved on 13 Mar 2023, 20:34
1/8