fix a crash in the server if a slave server is unregistered

This commit is contained in:
Volker Fischer 2011-11-14 18:30:16 +00:00
parent 64c50ece35
commit e0dc85a5f1

View File

@ -90,7 +90,7 @@ CServerListManager::CServerListManager ( const quint16 iNPortNum,
} }
} }
// per definition, the first entry in the server list it the own server // per definition, the first entry in the server list is the own server
ServerList.append ( ThisServerListEntry ); ServerList.append ( ThisServerListEntry );
// parse the predefined server infos (if any) according to definition: // parse the predefined server infos (if any) according to definition:
@ -234,8 +234,10 @@ void CServerListManager::OnTimerPollList()
{ {
QMutexLocker locker ( &Mutex ); QMutexLocker locker ( &Mutex );
// check all list entries except of the very first one (which is the central // Check all list entries except of the very first one (which is the central
// server entry) and the predefined servers if they are still valid // server entry) and the predefined servers if they are still valid.
// Note that we have to use "ServerList.size()" function in the for loop
// since we may remove elements from the server list inside the for loop.
for ( int iIdx = 1 + iNumPredefinedServers; iIdx < ServerList.size(); iIdx++ ) for ( int iIdx = 1 + iNumPredefinedServers; iIdx < ServerList.size(); iIdx++ )
{ {
// 1 minute = 60 * 1000 ms // 1 minute = 60 * 1000 ms
@ -316,7 +318,7 @@ void CServerListManager::CentralServerUnregisterServer ( const CHostAddress& Ine
// Find the server to unregister in the list. The very first list entry // Find the server to unregister in the list. The very first list entry
// must not be checked since this is per definition the central server // must not be checked since this is per definition the central server
// (i.e., this server), also the predefined servers must not be checked // (i.e., this server), also the predefined servers must not be checked.
for ( int iIdx = 1 + iNumPredefinedServers; iIdx < iCurServerListSize; iIdx++ ) for ( int iIdx = 1 + iNumPredefinedServers; iIdx < iCurServerListSize; iIdx++ )
{ {
if ( ServerList[iIdx].HostAddr == InetAddr ) if ( ServerList[iIdx].HostAddr == InetAddr )
@ -324,8 +326,11 @@ void CServerListManager::CentralServerUnregisterServer ( const CHostAddress& Ine
// remove this list entry // remove this list entry
ServerList.removeAt ( iIdx ); ServerList.removeAt ( iIdx );
// entry found, leave for-loop // entry found, leave for-loop (it is important to exit the
continue; // for loop since when we remove an item from the server list,
// "iCurServerListSize" is not correct anymore and we could get
// a segmentation fault)
break;
} }
} }
} }