0

My C# WPF app runs on regular basis and every now and then i keep getting this error attempt to read or write protected memory, this is often indication that other memory is corrupted c#

this is my code. Initially I thought some external app is interfering with my database so i added a code for checking if DB is closed or open. but error seem to somthing else this is driving me crazy.

I am posting the call stack and i see thread 17 has some facts on how the issue was caused.

Link for my drive where i have shared the entire dumpfile please check out thread no 17 [ https://drive.google.com/open?id=0BzWisplLq-PqMlhzaVdFNTJiZjg ]

please help me finding the issue ? i am new to this.enter image description here

using (var context = Context.Create("C:\\XSR_BIB_V2\\XSR_BIB_V2_DATABASE.sdf", "", 4091))
 {
  if (DbUpdateLoop.context.Database.Connection.State == System.Data.ConnectionState.Closed)  
                                              DbUpdateLoop.context.Database.Connection.Open();

           try
              {
             var data = DbUpdateLoop.context.EnergyPeakInfo_Tbl.Where(x => (my_startTime >= x.StartTime) && (my_stopTime <= x.StopTime)).FirstOrDefault();
               }
           catch (AccessViolationException aV)
            {
            //exception is not caught here
            }
            }
Anil Gadiyar
  • 399
  • 1
  • 2
  • 16
  • @ChrisO posted the stack. this might help me out if you could show me the way. – Anil Gadiyar Jan 02 '17 at 08:51
  • The dump you've posted doesn't look right or useful to me. How did you capture it? I would suggest running procdump, have it monitor your process for exceptions and have it write a dump when one is encountered. `procmon -n 1000 -ma -e 1 ` might get you started to get a useful dump. – Lieven Keersmaekers Jan 02 '17 at 17:49
  • 1
    The code you posted is not the code in your screenshot. You have not shared the entire dump file, you have only shared a text file. None of the commands confirms that the exception in the text file is actually an Access Violation. All in all, the request does not make sense. – Thomas Weller Jan 02 '17 at 18:47
  • @ThomasWeller brother please help me analyse this dmp file i uploaded newly. i am new to this and i will appreciated any hints from you [ https://drive.google.com/open?id=0BzWisplLq-PqMlhzaVdFNTJiZjg ] – Anil Gadiyar Jan 03 '17 at 07:08
  • @LievenKeersmaekers brother please help me analyse this dmp file i uploaded newly. i am new to this and i will appreciated any hints from you [ drive.google.com/open?id=0BzWisplLq-PqMlhzaVdFNTJiZjg ] – Anil Gadiyar Jan 03 '17 at 07:09
  • You have to take a crash dump at the time the exception happens. It's not sufficient to attach to the process before or after the exception has happened. The crash dump you provided is currently in a message loop, just as if everything is normal. – Thomas Weller Jan 03 '17 at 13:25
  • @ThomasWeller the dump i have taken is when the app was in hung state. i used prodump to get the dump file. when should i actually collect dump file ? – Anil Gadiyar Jan 03 '17 at 14:26
  • An exception and a hang are totally different things. One terminates the program, the other makes it never terminate. – Thomas Weller Jan 03 '17 at 14:29
  • @AnilGadiyar - According to your post you have sporadic Access Violations and not a hung application?! If that's the case, getting a proper dump would be to 1. start your application. 2. attach procdump and have it set up to take a dump for each exception encountered *`procdump -n 1000 -ma -e 1 `* 3. wait for the exception to happen. – Lieven Keersmaekers Jan 03 '17 at 19:27
  • @LievenKeersmaekers yes correct will do that and update you on – Anil Gadiyar Jan 04 '17 at 05:58

1 Answers1

2

AccessViolationException almost always indicates an unmanaged code was trying to read or write unmapped memory in the process. The only way managed code would throw this exception is by deliberately calling throw new AccessViolationException(), which managed code should never do. In your specific case, the exception is triggered by some DB library, apparently in unmanaged context. When this happens, you won't be able to catch the exception. Here is a section from MSDN that explains this:

AccessViolationException and try/catch blocks

Starting with the .NET Framework 4, AccessViolationException exceptions thrown by the common language runtime are not handled by the catch statement in a structured exception handler if the exception occurs outside of the memory reserved by the common language runtime. To handle such an AccessViolationException exception, you should apply the HandleProcessCorruptedStateExceptionsAttribute attribute to the method in which the exception is thrown. This change does not affect AccessViolationException exceptions thrown by user code, which can continue to be caught by a catch statement. For code written for previous versions of the .NET Framework that you want to recompile and run without modification on the .NET Framework 4, you can add the <legacyCorruptedStateExceptionsPolicy> element to your app's configuration file. Note that you can also receive notification of the exceptions if you have defined a handler for the AppDomain.FirstChanceException or AppDomain.UnhandledException event.

Basically it means that code that attempts to catch AV would not work:

try
{
    ... code that throws AV exception
}
catch (AccessViolationException ex)
{
    ... this will never be executed, exception will propagate to top level process handler
}

I don't recommend catching AV exception or applying HandleProcessCorruptedStateExceptionsAttribute to your assembly, unless you know how to recover from the exception, which you don't in your case, since you don't know what exact operation triggered this.

Now, to answer your original question: how to analyse the call stack for this exception. Since exception is unmanaged, you need to enable unmanaged debugging in VS, and collect unmanaged call stack when this exception is triggered.

seva titov
  • 11,720
  • 2
  • 35
  • 54