ketoo / NoahGameFrame

A fast, scalable, distributed game server engine/framework for C++, include the actor library, network library, can be used as a real time multiplayer game engine ( MMO RPG/MOBA ), which support C#/Lua script/ Unity3d, Cocos2dx and plan to support Unreal.

Home Page:https://github.com/ketoo/NoahGameFrame/wiki

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

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.