title: Random error: Attempted to read or write protected memory
body: <p>We have a  C# .Net application using WCF services. And the application is deployed in our production server under a Windows Service Application. One part of the module is responsible for creating shape files ((*.shp, *.dbf) for a smaller area the workers will be working today and send them down to a PDA.</p>
  
  <p>To write the shape files, we use a third party dll, NetTopologySuite</p>
  
  <pre><code>GisSharpBlog.NetTopologySuite.IO.ShapefileWriter
  </code></pre>
  
  <p>which is also in C#. <strong>(I am not sure whether any dll it reference use unmanaged code.)</strong>
  The system might work fine for a while say for a week. Then suddenly we get an exception saying</p>
  
  <pre><code>Attempted to read or write protected memory. 
  This is often an indication that other memory is corrupt.
  </code></pre>
  
  <p>from the Write method, where we write the geometry collection to shape files.</p>
  
  <pre><code>sfw.Write(FileName, new GeometryCollection(gc.ToArray()));
  </code></pre>
  
  <p><strong>(GeometryCollection is also from a third party dll, GeoAPI.dll)</strong></p>
  
  <p>This error brings down the whole service and makes it unfunctional. Then we would just restart the service and try to run the same data again, it would work fine for another week till it crash again. It happens only in production and at random times. We were not able to find the cause of the issue.</p>
  
  <p>Many forums suggest that it might be because of memory leaks in some unmanaged code. But we couldn't find which one. </p>
  
  <p>We are also ready to rewrite the part that create new shape files.
  Please help me to resolve this issue.
  Let me know if more details are required. Thanks in advance.</p>