diff --git a/include/libtorrent/aux_/session_impl.hpp b/include/libtorrent/aux_/session_impl.hpp index 3f0a714c6..de00f8ad0 100644 --- a/include/libtorrent/aux_/session_impl.hpp +++ b/include/libtorrent/aux_/session_impl.hpp @@ -382,6 +382,7 @@ namespace libtorrent // this pool is used to allocate and recycle send // buffers from. boost::pool<> m_send_buffers; + boost::mutex m_send_buffer_mutex; // the file pool that all storages in this session's // torrents uses. It sets a limit on the number of diff --git a/src/peer_connection.cpp b/src/peer_connection.cpp index 25de92b51..e9eb949f0 100755 --- a/src/peer_connection.cpp +++ b/src/peer_connection.cpp @@ -2607,6 +2607,7 @@ namespace libtorrent // return value is destructed buffer::interval peer_connection::allocate_send_buffer(int size) { + TORRENT_ASSERT(size > 0); char* insert = m_send_buffer.allocate_appendix(size); if (insert == 0) { diff --git a/src/session_impl.cpp b/src/session_impl.cpp index 3fa143e67..bb122e476 100755 --- a/src/session_impl.cpp +++ b/src/session_impl.cpp @@ -2388,7 +2388,11 @@ namespace detail std::pair session_impl::allocate_buffer(int size) { + TORRENT_ASSERT(size > 0); int num_buffers = (size + send_buffer_size - 1) / send_buffer_size; + TORRENT_ASSERT(num_buffers > 0); + + boost::mutex::scoped_lock l(m_send_buffer_mutex); #ifdef TORRENT_STATS m_buffer_allocations += num_buffers; m_buffer_usage_logger << log_time() << " protocol_buffer: " @@ -2400,8 +2404,12 @@ namespace detail void session_impl::free_buffer(char* buf, int size) { + TORRENT_ASSERT(size > 0); TORRENT_ASSERT(size % send_buffer_size == 0); int num_buffers = size / send_buffer_size; + TORRENT_ASSERT(num_buffers > 0); + + boost::mutex::scoped_lock l(m_send_buffer_mutex); #ifdef TORRENT_STATS m_buffer_allocations -= num_buffers; TORRENT_ASSERT(m_buffer_allocations >= 0);