Details
-
Bug
-
Resolution: Done
-
P2: Important
-
None
-
5.12.2
-
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
For Gerrit Dashboard: QTBUG-74882 | ||||||
---|---|---|---|---|---|---|
# | Subject | Branch | Project | Status | CR | V |
260606,8 | Fix crash for passing lambda with captures to QHttpServer::route | master | qt/qthttpserver | Status: MERGED | -2 | 0 |