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. [Closed] Passing QString's to C external libraries
QtWS25 Last Chance

[Closed] Passing QString's to C external libraries

Scheduled Pinned Locked Moved General and Desktop
4 Posts 3 Posters 3.4k 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.
  • L Offline
    L Offline
    Luc4
    wrote on last edited by
    #1

    Hi! I just noticed a behavior that is very strange when using QString's and an external C++ library. What I've done is simply to pass a QString to a function of an external library this way:

    @QString qString("test");
    const char* _qString = qString.toStdString().c_str();
    externalFunction(_qString);@

    What is happening is something very very strange, my suspect is that it's somehow related to implicit sharing (but I'm not sure). The output of this piece of code I place for debugging inside externalFunction(const char* clientPath):

    @fprintf(stderr, "1: %s.\n", clientPath);
    std::string rootPath, leafName, fileExt;
    char* _rootPath = new char[strlen(clientPath) + 1];
    strcpy(_rootPath, clientPath);
    fprintf(stderr, "_rootPath: %s.\n", _rootPath);
    rootPath = _rootPath;
    rootPath[0] = 'A';
    fprintf(stderr, "1b: %s.\n", clientPath);
    fprintf(stderr, "Address of clientPath: %x.\n", clientPath);
    fprintf(stderr, "Address of _rootPath: %x.\n", _rootPath);
    fprintf(stderr, "Address of rootPath: %x.\n", rootPath.c_str());@

    is:

    1: /home/luca/2.jpg.
    _rootPath: /home/luca/2.jpg.
    1b: Ahome/luca/2.jpg.
    Address of clientPath: 8751de4.
    Address of _rootPath: 874fcc8.
    Address of rootPath: 8751de4.

    Any idea why this is happening precisely and how I can avoid it? Is this related to Qt?
    Thanks!

    1 Reply Last reply
    0
    • ZlatomirZ Offline
      ZlatomirZ Offline
      Zlatomir
      wrote on last edited by
      #2

      The problem is this line: _const char* qString = qString.toStdString().c_str();, because it create a temporary std::string that will go out of scope before you can use it (so you will have a dangling pointer).

      You can achieve what you want something like this:
      @
      void c_func(char* str) {
      printf("%s\n", str);
      }

      int main()
      {
      QString s("Test QString to char pointer");

      char *ptr = strdup(s.toLocal8Bit().data()); //duplicate the returned c-style string

      c_func(ptr);
      free(ptr);

      QString s0("This also work");

      c_func(s0.toLocal8Bit().data()); //or make the temporary in your function call - this way the temp variable should be valid through the function call

      return 0;
      }
      @

      https://forum.qt.io/category/41/romanian

      1 Reply Last reply
      0
      • L Offline
        L Offline
        Luc4
        wrote on last edited by
        #3

        Indeed I solved using toAscii().constData(). Anyway I don't think it was going out of scope... The function I invoked was not asynchronous...

        1 Reply Last reply
        0
        • G Offline
          G Offline
          goetz
          wrote on last edited by
          #4

          See the "FAQ entry":http://developer.qt.nokia.com/faq/answer/how_can_i_convert_a_qstring_to_char_and_vice_versa

          Closing this thread for this reason.

          http://www.catb.org/~esr/faqs/smart-questions.html

          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