From ff70e3fbc25974bc5571e3c50696b22aa8000312 Mon Sep 17 00:00:00 2001 From: Eric House Date: Wed, 23 Jan 2013 07:35:20 -0800 Subject: [PATCH] fix use of map.insert: check for existing key and overwrite value --- xwords4/relay/devmgr.cpp | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/xwords4/relay/devmgr.cpp b/xwords4/relay/devmgr.cpp index 2069ede63..724222f65 100644 --- a/xwords4/relay/devmgr.cpp +++ b/xwords4/relay/devmgr.cpp @@ -38,8 +38,17 @@ DevMgr::Remember( DevIDRelay devid, const AddrInfo::AddrUnion* saddr ) logf( XW_LOGINFO, "%s(devid=%d)", __func__, devid ); time_t now = time( NULL ); UDPAddrRec rec( saddr, now ); + MutexLock ml( &m_mapLock ); - m_devAddrMap.insert( pair( devid, rec ) ); + + // C++'s insert doesn't replace, but the result tells whether the key was + // already there and provides an iterator via which it can be updated + pair::iterator, bool> result = + m_devAddrMap.insert( pair( devid, rec ) ); + if ( !result.second ) { + result.first->second = rec; + } + logf( XW_LOGINFO, "dev->addr map now contains %d entries", m_devAddrMap.size() ); }