Hi, I’m running into an interesting issue where I get an an unhandled exception from the Networking.dll file. The exception gets triggered after manually calling the garbage collector and looks like this:
#### Exception System.ObjectDisposedException - 0x00000000 (13) ####
#### Message:
#### System.Net.Sockets.NetworkStream::get_Length [IP: 0009] ####
#### System.Net.InputNetworkStreamWrapper::Read_HTTP_Line [IP: 004b] ####
#### System.Net.HttpListenerRequest::ParseHTTPRequest [IP: 000d] ####
#### System.Net.HttpListenerContext::get_Response [IP: 000d] ####
An unhandled exception of type 'System.ObjectDisposedException' occurred in GHIElectronics.TinyCLR.Networking.dll
I have an HTTP server running in the background and this exception will only trigger after I have made an HTTP request from my PC. I’m wondering if I’m not destroying an object properly so here is a sample of my HTTP server setup (running on its own thread):
///<summary> Async listener task. </summary>
private void HttpServerTask()
{
try
{
//Verify the previous HTTP connection is closed
if (_httpListener != null)
{
StopHttpServer();
}
Logger.LogMessage(Logger.Level.Info, _controllerId, $"Opening new HTTP server (IP: {EthernetController.GetIpAddressString()}, Port: 80)...");
_stopListening = false;
//Start a new HTTP listener
_httpListener = new HttpListener("http", 80); //http = 80, https = 443
_httpListener.Start();
int totalRequests = 0;
//Continuously listen until the network connection is closed
while (_httpListener != null && _httpListener.IsListening && !_stopListening && EthernetController.IsInitialized && EthernetController.LinkReady)
{
//Close any previous connections and wait for a new request (blocking)
_clientConnection?.Request?.Reset();
_clientConnection?.Response?.Close();
_clientConnection?.Reset();
_clientConnection = null;
_clientConnection = _httpListener.GetContext();
//Increment the total number of requests
totalRequests++;
Logger.LogMessage(Logger.Level.Debug, _controllerId, $"New request received from {_clientConnection.Request.RemoteEndPoint.Address}. Total session requests: {totalRequests}.");
Logger.LogMessage(Logger.Level.Debug, _controllerId, $"URL request: {EthernetController.GetIpAddressString()}{_clientConnection.Request.RawUrl}");
EthernetController.NetworkIsHealthy();
//Invoke the request received event
OnHttpRequestReceived.Invoke(_clientConnection.Request.RemoteEndPoint.Address, _clientConnection.Request.RawUrl.TrimEnd('/'));
}
}
catch (Exception ex)
{
//If the listener was not manually shut down, and the network is still available, restart the server end the current thread
if (!_stopListening && EthernetController.LinkReady)
{
Logger.LogMessage(Logger.Level.Error, _controllerId, $"HttpServerTask() failed - {ex.Message}.\r");
new Thread(StartHttpServer).Start();
}
}
}
Another exception in my SD card class! I’m wondering if running the garbage collector is causing problems … Note: I am using an SC20260D with extended heap.
SD Write Function:
public static bool WriteToFile(string filePath, byte[] dataToWrite, int offsetIndex, int numBytesToWrite)
{
try
{
//Check the SD card status
if (!SdCardIsAvailable())
{
Logger.LogMessage(Logger.Level.Warning, ControllerId, "WriteToFile() failed - SD card is not available.");
return false;
}
//Format the file path
string formattedPath = FormatFilePath(filePath);
//If the file to write should be within a directory, make sure the directory exists
if (!Directory.Exists(ExtractParentPath(formattedPath, '\\')))
{
Directory.CreateDirectory(ExtractParentPath(formattedPath, '\\'));
}
//If the data is empty, return true
if (dataToWrite == null || dataToWrite.Length == 0)
{
return true;
}
//Lock the thread while writing
lock (ThreadLock)
{
//Write the data to the file
using (FileStream writeStream = new FileStream(formattedPath, FileMode.Append))
{
writeStream.Write(dataToWrite, offsetIndex, numBytesToWrite); // <--- This line eventually causes final exceptions
writeStream.Flush(); // <--- This line hangs for 8-10 seconds
FileSystem.Flush(_sdCardController.Hdc);
writeStream.Close();
return true;
}
}
}
catch (Exception ex)
{
_sdCardIsConnected = false;
Logger.LogMessage(Logger.Level.Error, ControllerId, $"WriteToFile() failed - {ex.Message}, {ex.InnerException}, {ex.StackTrace}.\r");
_sdCardIsConnected = true;
return false;
}
}
After running the program for a while the WriteToFile() function speed suddenly screeches to a halt–specifically, the writeStream.Flush() line takes like 8-10 seconds to execute and this exception will get called exactly once when the slow down starts:
Added that line to the SD class and letting it run, so far no problems with the write function but will keep updated. Still having the HTTP/.dll exception though
Thank you!
I did some digging and I think it might be a visual studio setting called “Just My Code”? I must have enabled or disabled it at some point, or some other setting to be able to have the internal exception thrown while in debug mode. The code seems to run okay when not running in debug…