by Demez Christophe [cdemez2 at hotmail,com] posted on 2006/01/19 |
|
Hi everybody,
I have find a bug in SecureSocket and corrected it.
The Error : NullReferenceException
Why ?
-------
In "Thread 1" I call "public void ChangeSecurityProtocol(SecurityOptions options)"
In "Thread 2" I receive messages from a server.
I got a null reference because :
1 - m_Options has beeen changed
2 - m_Controller is NULL because not yet initialized
Solution
----------
1) Synchronize the update of m_Options and m_Controller.
lock (this){
m_Options = (SecurityOptions)options.Clone();
if(options.Protocol!=SecureProtocol.None)
{
if (this.Connected)
m_Controller = new SocketController(this, base.InternalSocket, options);
}
}
2) In EndReceive ( and some others... ), do this :
SocketController tempController;
lock (this)
tempController = m_Controller;
return tempController.BeginSend(buffer, offset, size, callback, state);
We use a temporary controller, that we lock for the time we set it (to avoid some others locks).
Chris ;-) |
by Pieter Philippaerts [Pieter at mentalis dot org] posted on 2006/01/22 |
|
If you look at the remarks section of the SecureSocket class, it states: "Any public static (Shared in Visual Basic) members of this type are safe for multithreaded operations. Any instance members are not guaranteed to be thread safe." So when you're calling instance methods of the SecureSocket class from multiple threads, it's your responsibility to synchronize the calls. |
by Demez Christophe [cdemez2 at hotmail dot com] posted on 2006/01/23 |
|
Yes, of course...
But the library receive a "EndReceive" from a server... and at the same time I do a call to change the security options.
I cannot lock the "EndReceive" method... then I got a crash.
Of course, I have synchronize all the methods, but you can only synchronize the EndReceive and the ChangeSecurity... methods.
Is it Right ? |