Skip to content
This repository has been archived by the owner. It is now read-only.

remove unused captures in lambdas #120

Open
wants to merge 1 commit into
base: master
from
Open

Conversation

@pboettch
Copy link
Contributor

@pboettch pboettch commented Apr 5, 2017

Found with clang's -Wunused-lambda-capture

@@ -217,7 +217,7 @@ namespace SimpleWeb {

///Use this function if you need to recursively send parts of a longer message
void send(const std::shared_ptr<Response> &response, const std::function<void(const boost::system::error_code&)>& callback=nullptr) const {
boost::asio::async_write(*response->socket, response->streambuf, [this, response, callback](const boost::system::error_code& ec, size_t /*bytes_transferred*/) {

This comment has been minimized.

@eidheim

eidheim Apr 5, 2017
Owner

response is needed here to keep the object alive until async_write is finished.

This comment has been minimized.

@pboettch

pboettch Apr 5, 2017
Author Contributor

That sounds error-prone.

This comment has been minimized.

@eidheim

eidheim Apr 5, 2017
Owner

Yes, but it has to be done somewhere since Boost.Asio is still quite low-level and does not support smart pointer parameters.

This comment has been minimized.

@pboettch

pboettch Apr 5, 2017
Author Contributor

Updated my pull-request. (added a comment, see below)

Found with clang's -Wunused-lambda-capture
@pboettch pboettch force-pushed the pboettch:master branch from 32561b1 to c477ab9 Apr 5, 2017
@@ -217,7 +217,8 @@ namespace SimpleWeb {

///Use this function if you need to recursively send parts of a longer message
void send(const std::shared_ptr<Response> &response, const std::function<void(const boost::system::error_code&)>& callback=nullptr) const {
boost::asio::async_write(*response->socket, response->streambuf, [this, response, callback](const boost::system::error_code& ec, size_t /*bytes_transferred*/) {
boost::asio::async_write(*response->socket, response->streambuf, [response, callback](const boost::system::error_code& ec, size_t /*bytes_transferred*/) {
(void) response; // response is not used here, but needs to captured to keep it alive because async_write is using response->*-fields

This comment has been minimized.

@eidheim

eidheim Apr 5, 2017
Owner

I would like not to have this line here. I think there is a comment somewhere that explains this, but not sure if this needs to be explained for every async call, as its already stated in the Boost.Asio reference. The reason response is captured is that its streambuf object (and the socket of course) needs to be kept alive. Sorry for my bad explanation above.

This comment has been minimized.

@pboettch

pboettch Apr 5, 2017
Author Contributor

Is it only the comment which disturbs you or also the (void) response;?

This comment has been minimized.

@eidheim

eidheim Apr 5, 2017
Owner

Mostly it's the (void) response;.

This comment has been minimized.

@pboettch

pboettch Apr 5, 2017
Author Contributor

Not having the (void) reponse; will lead to a warning in (at least) future versions of clang of this kind:

http/Simple-Web-Server/https_examples.cpp:96:41: warning: lambda capture 'server' is not used [-Wunused-lambda-capture]
    server.resource["^/work$"]["GET"]=[&server](shared_ptr<HttpsServer::Response> response, shared_ptr<HttpsServer::Request> /*request*/) {

(-Wunused-lambda-capture is activated with -Wall or -Wextra)

This comment has been minimized.

@eidheim

eidheim Apr 5, 2017
Owner

That is a good argument actually, but not sure if -Wunused-lambda-capture should be activated with -Wall or -Wextra. It might be an oversight by the clang++ developers, since using captures to prolong an shared_ptr's lifetime should be a valid use case, especially together with Boost.Asio. Not sure if the asio proposal to the C++ standard solves this in a different way. I'll have a look at it Tomorrow.

This comment has been minimized.

@eidheim

eidheim Apr 5, 2017
Owner

See for instance http://www.boost.org/doc/libs/1_63_0/doc/html/boost_asio/example/cpp11/echo/async_tcp_echo_server.cpp. Compiling this example would also result in the same warnings with -Wall -Wextra enabled on newer clang++ (if one of these two flags enable -Wunused-lambda-capture).

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Linked issues

Successfully merging this pull request may close these issues.

None yet

2 participants
You can’t perform that action at this time.