HttpListener and GC problem

Hi, I have an application which uses an HttpListener. I have build my own little Webserver.

Now the problem is when ever the GC collects I get an unhandled exception like this:

[quote]GC: 489msec 185592 bytes used, 7154076 bytes available
Type 0F (STRING ): 11904 bytes
Type 11 (CLASS ): 42792 bytes
Type 12 (VALUETYPE ): 576 bytes
Type 13 (SZARRAY ): 39432 bytes
Type 01 (BOOLEAN ): 24 bytes
Type 03 (U1 ): 648 bytes
Type 04 (CHAR ): 29196 bytes
Type 07 (I4 ): 36 bytes
Type 0C (R8 ): 1224 bytes
Type 0F (STRING ): 132 bytes
Type 11 (CLASS ): 8088 bytes
Type 12 (VALUETYPE ): 84 bytes
Type 15 (FREEBLOCK ): 7154076 bytes
Type 16 (CACHEDBLOCK ): 29064 bytes
Type 17 (ASSEMBLY ): 32508 bytes
Type 18 (WEAKCLASS ): 48 bytes
Type 19 (REFLECTION ): 192 bytes
Type 1B (DELEGATE_HEAD ): 720 bytes
Type 1C (DELEGATELIST_HEAD ): 96 bytes
Type 1D (OBJECT_TO_EVENT ): 288 bytes
Type 1E (BINARY_BLOB_HEAD ): 14232 bytes
Type 1F (THREAD ): 2304 bytes
Type 20 (SUBTHREAD ): 240 bytes
Type 21 (STACK_FRAME ): 3792 bytes
Type 22 (TIMER_HEAD ): 72 bytes
Type 26 (WAIT_FOR_OBJECT_HEAD): 48 bytes
Type 27 (FINALIZER_HEAD ): 3504 bytes
Type 28 (MEMORY_STREAM_HEAD ): 36 bytes
Type 29 (MEMORY_STREAM_DATA ): 396 bytes
Type 31 (IO_PORT ): 108 bytes
Type 34 (APPDOMAIN_HEAD ): 72 bytes
Type 36 (APPDOMAIN_ASSEMBLY ): 3168 bytes
#### Exception System.NullReferenceException - CLR_E_NULL_REFERENCE (75) ####
#### Message:
#### System.Net.OutputNetworkStreamWrapper::Flush [IP: 0014] ####
#### System.Net.HttpListenerResponse::System.IDisposable.Dispose [IP: 0018] ####
A first chance exception of type ‘System.NullReferenceException’ occurred in System.Http.dll
[/quote]

At the moment I generate for each request got from

 a new thread. This thread will know "render" the proper html code. After that a close the HttpListenerContext


```cs
private void ExecuteRequest()
{
     DateTime startReqeust = DateTime.Now;
     try
     {
          Microsoft.SPOT.Debug.Print("Request from: " + this.ctx.Request.RemoteEndPoint.Address.ToString() + "; url: " + this.ctx.Request.RawUrl);
          if (this.ctx.Request.HttpMethod == "GET")
          {
               this.ProcessHttpRequest();
          }
     }
     catch (Exception ex)
     {
          Microsoft.SPOT.Debug.Print("Error while processing the request \r\n" + ex.ToString());
     }
     if (this.ctx != null)
     {
          try
          {
               Microsoft.SPOT.Debug.Print("Request \"" + this.ctx.Request.RawUrl + "\" closed after: " + (DateTime.Now - startReqeust).ToString());
               this.ctx.Close();
          }
          catch
          {
               Microsoft.SPOT.Debug.Print("Error while context close");
          }
          this.ctx = null;
     }
}

Is there any trick? What could be the problem?

With NETMF 4.3 QFE1 and GHI SDK 2014 R5

Thx for all replies

@ jp_miata - It sounds like this issue: https://netmf.codeplex.com/workitem/2158

3 Likes

@ John - Thanks for the hint.

I solved the problem, by downloading the System.Http source files from codeplex and add following if statement to OutputNetworkStreamWrapper, Flush Method

 if (this.m_Stream != null)
        this.m_Stream.Flush();
2 Likes

Yes I face this problem too and solved in the same way.
Apparently GC kill the networkstream to null. So the call to the Close() crash miserably.

Hello,
I have the same issue. I’m trying to recompile the System.HTTP library but without success. Could someone tell me how I can do this or could send me the .dll file.
Thanks.

How can i find the link for the system.http projekt?