Skip to content

Commit

Permalink
sync code from maock: fix memory leak when connection is down
Browse files Browse the repository at this point in the history
  • Loading branch information
wallyatgithub committed Nov 7, 2022
1 parent 5c3c8ed commit 554417c
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 2 deletions.
5 changes: 5 additions & 0 deletions asio_server_base_handler.cc
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,11 @@ base_handler::~base_handler()
handler_io_service.erase(this_handler_id);
}

void base_handler::reset_writefun()
{
auto tmp = std::move(writefun_);
}

base_handler* base_handler::find_handler(uint64_t handler_id)
{
auto it = alive_handlers.find(handler_id);
Expand Down
2 changes: 2 additions & 0 deletions asio_server_base_handler.h
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,8 @@ class base_handler

uint64_t get_handler_id();

void reset_writefun();

protected:
std::map<int32_t, std::shared_ptr<asio_server_stream>> streams_;
connection_write writefun_;
Expand Down
1 change: 1 addition & 0 deletions asio_server_connection.h
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,7 @@ class asio_server_connection : public std::enable_shared_from_this<asio_server_c
boost::system::error_code ignored_ec;
socket_.lowest_layer().close(ignored_ec);
deadline_.cancel();
handler_->reset_writefun();// break the cyclic reference, as base_handler::writefun_ holds shared_ptr to this connnection
}

private:
Expand Down
5 changes: 4 additions & 1 deletion asio_server_http1_handler.cc
Original file line number Diff line number Diff line change
Expand Up @@ -424,7 +424,10 @@ int http1_handler::on_write(std::vector<uint8_t>& buffer, std::size_t& len)
void http1_handler::initiate_write()
{
write_signaled_ = false;
writefun_();
if (writefun_)
{
writefun_();
}
}

void http1_handler::stream_error(int32_t stream_id, uint32_t error_code)
Expand Down
5 changes: 4 additions & 1 deletion asio_server_http2_handler.cc
Original file line number Diff line number Diff line change
Expand Up @@ -416,7 +416,10 @@ void http2_handler::signal_write() {

void http2_handler::initiate_write() {
write_signaled_ = false;
writefun_();
if (writefun_)
{
writefun_();
}
}

void http2_handler::stream_error(int32_t stream_id, uint32_t error_code) {
Expand Down

0 comments on commit 554417c

Please sign in to comment.