写c++循环删除map,list等元素时,需要注意的细节:
// 这里的调用是在同一个线程里的,所以这里不用考虑互斥处理。
tagRtspData* item = NULL;
map<SSocketKey, tagRtspData>::iterator it;
for (it=m_mapRtsp.begin(); it!=m_mapRtsp.end(); )
{
item = &it->second;
const SSocketKey *sSktKey = &it->first;
// 数据项为空,直接跳过
if (NULL == item) //不可能为空 !!!
{
m_mapRtsp.erase(it++);
LogMessage(CM_LOG_WARN, "item is null, continue line.");
continue;
}
if(*tm - item->lastTime.tv_sec > RTSP_TIMEOUT_TIME) //判断是否超时
{
LogMessage(CM_LOG_NOTICE, "RTSP timeout, RTSP sIp=%x, dIp=%x, map size=%d",
sSktKey->SIP, sSktKey->DIP, m_mapRtsp.size());
//处理RTSP 数据
m_mapRtsp.erase(it++);
continue;
}
++it;
}
std::map<RTC::RtpStreamRecv*, uint32_t> mapRtpStreamMappedSsrc;