Permalink
Browse files

Properly unbind() to cancel handler and close the socket

This is necessary to get rid of the allocated memory to make valgrind no
longer complain.

Signed-off-by: Jan Blunck <jblunck@infradead.org>
  • Loading branch information...
jblunck committed Nov 8, 2011
1 parent 56878cf commit 751e0eef9c991c24b1dddc4775a9e74c1879c81f
@@ -99,7 +99,8 @@ class datagram_receiver : boost::noncopyable,
size_t length)
{
if (error) {
- _status.error(error.message());
+ if (error != boost::asio::error::operation_aborted)
+ _status.error(error.message());
return;
}
@@ -82,6 +82,8 @@ class buffered_datagram_writer
* we do not wait in run() infinitely. But poll() all ready work
* first.
*/
+ _socket.cancel();
+ _socket.close();
_socket.get_io_service().poll();
_socket.get_io_service().stop();
return;
@@ -145,6 +147,11 @@ class local_datagram_receiver :
base_type::start_receive(_socket);
}
+ void unbind(const unsigned short port)
+ {
+ _socket.cancel();
+ _socket.close();
+ }
};
#endif // __LOCAL_DATAGRAM_RECEIVER_HPP__
@@ -27,5 +27,13 @@ int main()
receiver->run();
uint64_t t = s.elapsed_ns();
std::cout << ( MESSAGES_NR * 1e9 ) / t << " msg/s" << std::endl;
+
+ // The following is necessary to free all used memory:
+ // 1.) cancel the handler
+ receiver->unbind(12345);
+
+ // 2.) execute the handler to process cancelation
+ receiver->get_io_service().reset();
+ receiver->get_io_service().poll();
return 0;
}

0 comments on commit 751e0ee

Please sign in to comment.