Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Random error: Attempted to read or write protected memory

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.

To write the shape files, we use a third party dll, NetTopologySuite

GisSharpBlog.NetTopologySuite.IO.ShapefileWriter

which is also in C#. (I am not sure whether any dll it reference use unmanaged code.) The system might work fine for a while say for a week. Then suddenly we get an exception saying

Attempted to read or write protected memory. 
This is often an indication that other memory is corrupt.

from the Write method, where we write the geometry collection to shape files.

sfw.Write(FileName, new GeometryCollection(gc.ToArray()));

(GeometryCollection is also from a third party dll, GeoAPI.dll)

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.

Many forums suggest that it might be because of memory leaks in some unmanaged code. But we couldn't find which one.

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.

like image 500
franklins Avatar asked Mar 12 '13 14:03

franklins


2 Answers

In my experience, that message was a result of a memory leak. This is what I'd do if I am in your situation especially since you are working on a third-party DLL.

1) Monitor your WCF server and see what is going on with the DLLHost.exe and the aspnet services in the task manager. I have a feeling that your third-party DLL has a memory leak that causes these 2 services to bloat and reach the limit of your servers memory. This is the reason why it works for a while and then suddenly just stopped working.

2) Identify a good schedule on when you can recycle your servers memory and application pool. Since the issue is rampant, you might want to do this every midnight or when no one is actively using it.

3) Write a good error logging code to know exactly what is happening during the time it bogged down. I would put the following information on the error logs: The parameters that you are passing, the user who encountered that problem etc. This is so you will know exactly what is happening.

4) Check the Event Viewer as maybe there is some information in there that can pinpoint the problem.

4) After doing 1, 2, and 3 and I will call your third-party DLL vendor and see what they can do to help you. You might need to provide the information that you collected from 1, 2, 3 and 4 items from above.

Good luck and I hope this will help.

like image 158
PM_ME_YOUR_CODE Avatar answered Oct 25 '22 12:10

PM_ME_YOUR_CODE


I think you have some unmanaged code in the third libraries that is getting an address protected by the system or used by other applications.

like image 36
Hussein Zawawi Avatar answered Oct 25 '22 13:10

Hussein Zawawi