akakeiz / lidgren-network-gen3

Automatically exported from code.google.com/p/lidgren-network-gen3
0 stars 0 forks source link

improper use of m_handshakes dictionary #62

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
in NetPeer.Internal.cs lines 167-168 (rev 190) you do:

foreach (NetConnection conn in m_handshakes.Values )
    conn.Shutdown(m_shutdownReason);

which leads to a call to ExecuteDisconnect in NetConnection.Handshake.cs
where you do

m_peer.m_handshakes.Remove(m_remoteEndpoint);

this can (and in my context does) throw an exception. basically, you are 
changing the collection while you enumerate it (foreach uses the IEnumerator 
interface). i haven't looked at all the details (i'm a first time user of your 
lib) but you probably need to use the good old for loop in this case.

the exception message is "Collection was modified; enumeration operation may 
not execute." which is a bit cryptic at first glance but if you search for it, 
you'll find good info.

thanks for sharing your library!

Original issue reported on code.google.com by xy5...@gmail.com on 10 Mar 2011 at 12:35

GoogleCodeExporter commented 9 years ago
i have replaced it with this

lock (m_handshakes)
{
//  foreach (NetConnection conn in m_handshakes.Values )
//  conn.Shutdown(m_shutdownReason);

// create a temporary list
List<NetConnection> conns = new List<NetConnection>();
foreach( NetConnection conn in m_handshakes.Values )
{
    conns.Add( conn );
}
// iterate through the list. this is safe since the list will not be modified 
during // enumeration. only the dictionary will.
foreach( NetConnection conn in conns )
{
    conn.Shutdown( m_shutdownReason );
}

}

which gets rid of the exception but i don't understand the rest of your code 
enough (you have similar dictionaries and lists of connections) to determine if 
this is a full solution or not.
you may want to check your use of all of your dictionaries for similar issues.

Original comment by xy5...@gmail.com on 10 Mar 2011 at 1:15

GoogleCodeExporter commented 9 years ago
Nice catch! Fixed in rev 191 - thanks!

Original comment by lidg...@gmail.com on 10 Mar 2011 at 6:45