Uploaded image for project: 'Qt'
  1. Qt
  2. QTBUG-74882

QHttpServer crashes when lambda catches `this`

    XMLWordPrintable

Details

    • Bug
    • Resolution: Done
    • P2: Important
    • None
    • 5.12.2
    • Network
    • None

    Description

      I encountered a problem when using QtHttpServer. If lambda (registered for a route) catches `this`, program ends in undefined behavior. Example to show how to reproduce this:

      class A
      {
        QtHttpServer server;
        int data;
        A() 
        {
          server.route("/", [this]()
          {
            qDebug() << data;
          });
          server.listen(QHostAddress::Any);
        }
      }

      This happens, because arguments of function route are perfectly forwarded deeper. Then in routeImpl the lambda is caught by reference when creating a rule. The problem is, the temporary lambda created in above example stops existing as soon as line server.route(...); ends. As a result we end up with a dangling reference in Rule object. It still works for cases where our lambda doesn't rely on accessing external variables (see example here: https://rextester.com/HGDT10316 ), but it still isn't something that should be done.

      For my project where I'm using QtHttpServer I used following workaround: when creating new Rule in routeImpl I don't capture viewHandler by reference, but by value. This way I can be sure that the lambda was copied and won't be deleted prematurely.

      Attachments

        No reviews matched the request. Check your Options in the drop-down menu of this sections header.

        Activity

          People

            mikhailsvetkin Mikhail Svetkin
            marpan Marek Panek
            Votes:
            2 Vote for this issue
            Watchers:
            6 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved:

              Gerrit Reviews

                There are no open Gerrit changes