QString to std:string crash using 3rd party library [Solved]
-
Hi all. I'm having a funky problem that I don't seem to be able to solve. I'm calling a 3rd party library method with the following signature:
@unsigned char libxbee::Con::Tx(std::string data);@
Calling the method with a string literal poses no problem:
@connection->Tx("Hi there!");@
But since I'm composing a string with values from a database, It surely makes sense to use QString and then convert to std::string:
@std::string str = message.toStdString();
connection->Tx(str);@However, this makes the 3rd party library code die because of an exception. Printing out the std::str string above shows no trouble at all.
I've tried all sorts of combinations - direct initialization, constructing the std::string from String::toLocal8Bit().data() and other combinations that makes sense to me - but same result.
Why does a literal work and not the QString::toStdString() conversion (or other flavors)? Any workarounds? Suggestions? Please?
-
Have you got the problems only with composed strings from the database?
What happens if you try the same simple string here?@QString message = QString("Hi there!");
std::string str = message.toStdString();
connection->Tx(str);@Can you use a std::string on its own? (= passing a string instead of ASCII literal)
@std:string str = std::string("Hi there!");
connection->Tx(str);@ -
Edit: Was a bit too quick on the keyboard.
Using a std::string argument works just fine.
However, you nailed it with the database content! If I supply a simple "Hi there" QString, the conversion works just as expected.
But - I still don't understand why it fails. My initial suspicion was UTF-8 encoding, but transforming the QString with toLatin1() or toLocal8Bit() doesn't help.
-
I remember boost::filesystem::file_size having problems and crashing on a Mac. I used .toStdWString() then.
Maybe if you enter your database string by hand instead of "Hi there!", like@QString message = QString("YOURDATABASECONTENT");
std::string str = message.toStdString();
connection->Tx(str);@I bet it's some "special" character (conversion) or string length. Maybe you can track down the bad character and conclude from there which conversion would be best.
-
Sorry for not getting back earlier.
I think I have solved the problem - I believe it was related to the state of the unit I was trying to communicate with (needed a reset), combined with the length of the string (the unit buffer is limited). Nothing wrong with the string conversion as suspected at first.
Thanks for your inputs!