Question

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.

Was it helpful?

Solution

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.

OTHER TIPS

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.

You have an Access Violation (pointer to memory not belonging to your application space, including null/mass - 0x0 - address) in one of your third-party DLLs.

Or else, it's maybe some unmanaged COMObject you're using that causes this error.

The random nature of this error, would suggest to me that it may be a matter of threads. Specifically the Write method of ShapefileWriter might have been called, got delayed in a thread then you call Close. The delayed Write method then tries to write over a closed (and protected) file, which could result in the error you see.

This is purely speculation since there's not much code to make a better guess, but I've experienced this issue using video writing libraries, so it might be the same in your case.

Check to make sure you don't have threads within threads. That is what happened when I encountered this error. See this link for more information: Attempted to read or write protected memory. This is often an indication that other memory is corrupt

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top