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. QString to std:string crash using 3rd party library [Solved]
Forum Updated to NodeBB v4.3 + New Features

QString to std:string crash using 3rd party library [Solved]

Scheduled Pinned Locked Moved General and Desktop
5 Posts 2 Posters 2.6k Views 1 Watching
  • 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.
  • J Offline
    J Offline
    janfaroe
    wrote on last edited by
    #1

    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?

    1 Reply Last reply
    0
    • H Offline
      H Offline
      hardcodes.de
      wrote on last edited by
      #2

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

      while(!sleep){++sheep;}

      1 Reply Last reply
      0
      • J Offline
        J Offline
        janfaroe
        wrote on last edited by
        #3

        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.

        1 Reply Last reply
        0
        • H Offline
          H Offline
          hardcodes.de
          wrote on last edited by
          #4

          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.

          while(!sleep){++sheep;}

          1 Reply Last reply
          0
          • J Offline
            J Offline
            janfaroe
            wrote on last edited by
            #5

            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!

            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