This project is read-only.
3
Vote

System.ObjectDisposedException in class NetBaseStream

description

This issue is against the NetSockets version 1.2 source code (change set 47519 ).
 
The ThreadedReceive() method of class NetBaseStream has code that can result in a System.ObjectDisposedException. Method Stop(NetStoppedReason) sets IsActive to false, and then calls stream.Stop(). Calling stream.Stop() disposes the stream object, so it is no longer valid for use. Despite this method ThreadedReceive() (which runs on a separate thread to method Stop()) accesses stream without any checks.
 
Instead of using
while (IsActive && stream.CanRead)
I suggest a new local bool for method ThreadedReceive() to which stream.CanRead is assigned. Then, at the very end of the while loop add a try/catch(ObjectDisposedException). In the try assign stream.CanRead to the new bool. If the exception is called, break out of the while loop.
 
I would think that most people don't see this exception as it occurs when everything is closing down. We are running some unit tests (using NUnit) which can throw ObjectDisposedExceptions after the test has passed. This means that even though our unit tests are all passing, an ObjectDisposedException is still occurring which is undesirable.

file attachments

comments

SJazza wrote Aug 8, 2011 at 10:26 AM

I've made a fix (patch diff file) based on the suggestion using the implementation in change set 56676

wrote Aug 8, 2011 at 12:24 PM

SJazza wrote Aug 8, 2011 at 12:24 PM

Another fix (patch2 diff file) better suited to the suggestion (covering only the scope of the stream) using the implementation in change set 56676.

wrote Feb 14, 2013 at 1:25 AM