-
Notifications
You must be signed in to change notification settings - Fork 648
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
valgrind inspired fixes #1693
valgrind inspired fixes #1693
Conversation
very nice, i wanted to run valgrind a while back and was unsuccessful. care to explain a bit the errors you were having and how the changes fixes them. thanks. |
Related bitshares/bitshares-fc#118 Some valgrind output added into this pull requests will help. I also think we might be able to get some better or at least different results profiling than with the gprof. A wiki on how you are doing this will be very helpful IMHO. |
fix referencing local stack variable in async threadValgrind reported both invalid reads and invalid writes with traces like this:
The original code created a local variable on stack and deferred a lambda function reference-capturing the local variable into an asynchronous thread. At the time the lambda function was called, it used already invalidated memory. What is worse, it could rewrite a stack in use. I have put the object into a shared_ptr and capture it in the lambda function by copy. After the last commit I have tested it again and compared the logs from valgrind. Definitely fixed. explicitly cleanup external library facilitiesVarious memory leaks are detected with malloc being called from
The proposed proper cleanup shrinks the number of the reported memory leaks, yet some of them remain. |
I will comment the commits in bitshares/bitshares-fc#118 later, probably tomorrow. |
Does this seem like it can make it into 3.1.0? If yes, please add to the Project Board and Milestone, else to the Backlog. I don't want to loose track of it. |
Thank you.
I think is possible, Added. |
@@ -4919,7 +4920,7 @@ namespace graphene { namespace net { namespace detail { | |||
return _node_delegate->method_name(__VA_ARGS__); \ | |||
} \ | |||
else \ | |||
return _thread->async([&](){ \ | |||
return _thread->async([&, statistics_collector](){ \ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please remove the &
and capture this
instead. Same below.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am sorry, but this won't work.
Strictly speaking, it would be enough to capture by copy only this->_node_delegate
. But omitting the implicit by-reference capture (i.e. &
) the __VA_ARGS__
won't be captured.
Well, the simplest solution is to use the implicit capture by copy (i.e. =
), which won't compile because the functions used with the macro consume parameters as references (see the interface class node_delegate
), at least one uses such parameter as an additional output parameter.
Furthermore, it is not easily seen that the original caller provides these references as of non-stack allocated entities. Thus, to be completely bug-proof, all of these parameters shall be passed in shared_ptr
.
Now, from the valgrind output I have produced, it does not seem this is the case. I suppose the proposed modification to be accepted as is (it provably solves an existing problem), and in the case a strictly complete solution is deemed required, a specific issue to be opened for the required refactor.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Missed the VA_ARGS, sorry.
handle_message
, for example, uses a stack-allocated message
(originating from message_oriented_connection_impl::read_loop
). Normally this lives longer than the handle_message
call should take, however, it is re-used when reading the next message (which could lead to data corruption), and it is destroyed when reading fails (e. g. when the connection breaks down). This could even be related to #1256 @jmjatlanta ?
I agree that this should be handled in another ticket.
Nice catch,thanks! |
Hey Monkeys!
I had some troubles and run the witness_node with valgrind (the memcheck tool). As a result, I provide here some minor fixes.
Ape out!