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



  • 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;
    }
    


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



  • @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.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.