Bug or not? in Map iterator
opened this issue · comments
This code crash the server.
If use RemoveElement in iterator, server crashed after try call code: pServerData = mLoginMap.Next();
It's bug?
p.s. if remove code mLoginMap.RemoveElement he is not crashed. I'm think, iterator broken after RemoveElement from map
NF_SHARE_PTR<NFrame::EServerData> pServerData = mLoginMap.First();
while (pServerData)
{
if (nAddress == pServerData->GetSocket())
{
printf("\nLOGIN SERVER DISCONNECTED AND REMOVED FROM LISt");
mLoginMap.RemoveElement(pServerData->GetID());
}
pServerData = mLoginMap.Next();
}
I searched NF's code and I didn't find any code like that, did you change the code like that?
The code I found is:
int nServerID = 0;
pServerData = mLoginMap.First();
while (pServerData)
{
if (nAddress == pServerData->nFD)
{
nServerID = pServerData->pData->server_id();
break;
}
pServerData = mLoginMap.Next();
}
mLoginMap.RemoveElement(nServerID);
You can't write code like that as std:: iterator can't remove element like that when looping.
If you want remove a element when looping you would write code as below:
for (it = map.begin(); it != map.end(); )
{
if (nAddress == pServerData->GetSocket())
{
it = map.earse(it)
}
else
{
it++
}
}
i'm use NFMapEx
I know, you cannot remove an element when looping.