Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Unsolved Crash when using QString and va_list under x86 (x64 works!)

    General and Desktop
    2
    3
    858
    Loading More Posts
    • 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
      Justin Sayne last edited by

      Does anyone have a moderate idea why the following works perfectly fine and as expected under x64 but crashes on the line indicated under x86? Having a look at argp in the debugger, argp already seems wrong because it contains character -52 like 100 times in a row whereas under x64 it has just around 8 reasonable and positive characters.
      Is there a work-around where I possibly don't have to change the method signature?

      #include <QString>
      
      static void test(const QString& format, ...)
      {
        static char msg[2048];
        
        va_list argp;
        va_start(argp, format);
        // crashes here under x86
        vsnprintf(msg, 2048, format.toStdString().c_str(), argp);
        va_end(argp);
        printf("%s", msg);
      }
      
      int main()
      {
        test("teststring: %s", "foobar");
        return 0;
      }
      

      The funny thing is that this also works perfectly fine under both configurations...

      static void test(const char* format, ...)
      {
        static char msg[2048];
        
        va_list argp;
        va_start(argp, format);
        vsnprintf(msg, 2048, format, argp);
        va_end(argp);
        printf("%s", msg);
      }
      
      int main()
      {
        QString qstr = "teststring: %s";
        test(qstr.toUtf8().data(), "foobar");
        return 0;
      }
      
      1 Reply Last reply Reply Quote 0
      • K
        KeithS last edited by

        You can't pass a reference to the format for va_start(). Use 'const QString format' instead of 'const QString& format'.

        J 1 Reply Last reply Reply Quote 0
        • J
          Justin Sayne @KeithS last edited by

          @KeithS
          Wow, thanks. After knowing the keyword I finally found the explanation and it completely makes sense. I'm now just wondering why it worked perfectly fine under x64, and not even by accident because this is a log function of our framework so it's used all over the place.

          1 Reply Last reply Reply Quote 0
          • First post
            Last post