diff --git a/libtorrent/src/peer_connection.cpp b/libtorrent/src/peer_connection.cpp index dc4b294a5..77ff59d93 100755 --- a/libtorrent/src/peer_connection.cpp +++ b/libtorrent/src/peer_connection.cpp @@ -1707,6 +1707,8 @@ namespace libtorrent TORRENT_ASSERT(block.block_index < t->torrent_file().piece_size(block.piece_index)); TORRENT_ASSERT(!t->picker().is_requested(block) || (t->picker().num_peers(block) > 0)); TORRENT_ASSERT(!t->have_piece(block.piece_index)); + TORRENT_ASSERT(std::find(m_download_queue.begin(), m_download_queue.end(), block) == m_download_queue.end()); + TORRENT_ASSERT(std::find(m_request_queue.begin(), m_request_queue.end(), block) == m_request_queue.end()); piece_picker::piece_state_t state; peer_speed_t speed = peer_speed(); diff --git a/libtorrent/src/session_impl.cpp b/libtorrent/src/session_impl.cpp index 209e833c3..a93ad7cb0 100755 --- a/libtorrent/src/session_impl.cpp +++ b/libtorrent/src/session_impl.cpp @@ -1263,10 +1263,21 @@ namespace detail torrent& t = *i->second; if (t.want_more_peers()) { - if (t.try_connect_peer()) + try { - --max_connections; - steps_since_last_connect = 0; + if (t.try_connect_peer()) + { + --max_connections; + steps_since_last_connect = 0; + } + } + catch (std::bad_alloc&) + { + // we ran out of memory trying to connect to a peer + // lower the global limit to the number of peers + // we already have + m_max_connections = num_connections(); + if (m_max_connections < 2) m_max_connections = 2; } } ++m_next_connect_torrent; @@ -2406,8 +2417,10 @@ namespace detail m_buffer_usage_logger << log_time() << " protocol_buffer: " << (m_buffer_allocations * send_buffer_size) << std::endl; #endif - return std::make_pair((char*)m_send_buffers.ordered_malloc(num_buffers) + std::pair ret((char*)m_send_buffers.ordered_malloc(num_buffers) , num_buffers * send_buffer_size); + if (ret.first == 0) throw std::bad_alloc(); + return ret; } void session_impl::free_buffer(char* buf, int size)