diff --git a/src/net.cpp b/src/net.cpp index f7e2ae601..c726f3714 100644 --- a/src/net.cpp +++ b/src/net.cpp @@ -671,12 +671,23 @@ void SocketSendData(CNode *pnode) static list vNodesDisconnected; -static bool ReverseCompareNodeMinPingTime(CNode *a, CNode *b) +class CNodeRef { +public: + CNodeRef(CNode *pnode) : _pnode(pnode) {_pnode->AddRef();} + ~CNodeRef() {_pnode->Release();} + + CNode& operator *() const {return *_pnode;}; + CNode* operator ->() const {return _pnode;}; +private: + CNode *_pnode; +}; + +static bool ReverseCompareNodeMinPingTime(const CNodeRef &a, const CNodeRef &b) { return a->nMinPingUsecTime > b->nMinPingUsecTime; } -static bool ReverseCompareNodeTimeConnected(CNode *a, CNode *b) +static bool ReverseCompareNodeTimeConnected(const CNodeRef &a, const CNodeRef &b) { return a->nTimeConnected > b->nTimeConnected; } @@ -691,7 +702,7 @@ public: GetRandBytes(vchSecretKey.data(), vchSecretKey.size()); } - bool operator()(CNode *a, CNode *b) + bool operator()(const CNodeRef &a, const CNodeRef &b) { std::vector vchGroupA, vchGroupB; CSHA256 hashA, hashB; @@ -714,7 +725,7 @@ public: }; static bool AttemptToEvictConnection(bool fPreferNewConnection) { - std::vector vEvictionCandidates; + std::vector vEvictionCandidates; { LOCK(cs_vNodes); @@ -727,7 +738,7 @@ static bool AttemptToEvictConnection(bool fPreferNewConnection) { continue; if (node->addr.IsLocal()) continue; - vEvictionCandidates.push_back(node); + vEvictionCandidates.push_back(CNodeRef(node)); } } @@ -755,8 +766,8 @@ static bool AttemptToEvictConnection(bool fPreferNewConnection) { // Identify CNetAddr with the most connections CNetAddr naMostConnections; unsigned int nMostConnections = 0; - std::map > mapAddrCounts; - BOOST_FOREACH(CNode *node, vEvictionCandidates) { + std::map > mapAddrCounts; + BOOST_FOREACH(const CNodeRef &node, vEvictionCandidates) { mapAddrCounts[node->addr].push_back(node); if (mapAddrCounts[node->addr].size() > nMostConnections) {