Random error: Attempted to read or write protected memory
Asked Answered
C

5

11

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.

Clarabelle answered 12/3, 2013 at 14:43 Comment(3)
And your question is?Hecate
Can you attach a debugger and look at the call stack?Nada
It's not a memory leak, it' a GPF en.wikipedia.org/wiki/General_protection_fault or in other terms, a bug in the native world (for example, an equivalent to the NullReferenceException in .NET). Check out all the native 3rd parties DLL you're using. One of them has a bug, or you're not using it the way it expects, and it's not telling you nicelyAmaranthine
B
8

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.

Buller answered 12/3, 2013 at 16:33 Comment(0)
S
1

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.

Schreibe answered 26/3, 2013 at 20:12 Comment(0)
C
0

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.

Conaway answered 28/3, 2013 at 10:29 Comment(1)
To anyone who downvoted this, why? My answer is absolutely correct, as I don't have a proper look to the whole codebase, I can't say much more about this case. Thus giving necessary leads as to how to resolve this problem.Conaway
T
-1

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.

Tami answered 27/3, 2013 at 12:47 Comment(0)
A
-1

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

Atropos answered 8/8, 2013 at 17:32 Comment(2)
was it consistent or did happen at random times say after 2 weeks or 3 weeks like in my case?Clarabelle
Ours was fairly consistent. Once we knew what it was, we could get this exception to happen within minutes of starting the application.Atropos

© 2022 - 2024 — McMap. All rights reserved.