Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Attempted to read or write protected memory in Oracle 11g with ODP.NET

I am developing an application that is supposed to run for long periods and make extensive usage of an Oracle (11g) database via ODP.NET.

It happens, though, that once in a while (every 2 or 3 days) a System.AccessViolationException is thrown by ODP.NET and then the application needs to be restarted. Here is my stack trace:

Unhandled exception: System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. 
---> System.AccessViolationException: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
at Oracle.DataAccess.Client.OpsSql.Prepare2(IntPtr opsConCtx, IntPtr& opsErrCtx, IntPtr& opsSqlCtx, IntPtr& opsDacCtx, OpoSqlValCtx*& pOpoSqlValCtx, string pCommandText, IntPtr& pUTF8CommandText, OpoMetValCtx*& pOpoMetValCtx, Int32 prmCnt)
at Oracle.DataAccess.Client.OracleCommand.ExecuteNonquery()

The rest of the stack trace is different from time to time and refers to internal calls from my application.

Now, I've made a fair amount of research before asking here, but I have found nothing conclusive. A number of other people are apparently experiencing the very same problem, although the root causes seem to vary a lot. I really hope somebody has a solution for this one :-)

On an unrelated note, it appears that this exception is capable of ignoring my catch {} blocks and result in an application crash every time it occurs. Is that because it is related to memory corruption issues?

Regards, Andrea

edit: further investigation led me to believe that it could be worth starting the "Distributed Transactions Coordinator" service and see if the exception stops being thrown. What do you think?

like image 623
Andrea Avatar asked Jul 01 '11 15:07

Andrea


3 Answers

This is a bug. The 11.1 and 11.2 providers had this issue. The only way to get around this is to install the 11.2.0.2 client and then apply patch 6.

like image 108
tsells Avatar answered Jan 18 '23 03:01

tsells


While constructing my OracleCommand object and adding parameters...

I found that changing from:

select.Parameters.Add("Result", OracleDbType.RefCursor);

To:

select.Parameters.Add("Result", OracleDbType.RefCursor, ParameterDirection.Output);

Eliminated this problem for me on the 11.2.0.2 client.

like image 26
Mike Mayer Avatar answered Jan 18 '23 02:01

Mike Mayer


We experienced the same AccessViolationException because an RefCursor was declared as an input parameter instead of Output.

command.Parameters.Add("O_RECS", OracleDbType.RefCursor, null, ParameterDirection.Input);

This is a harsh message for such a simple mistake. Changing the parameter direction fixed the error.

command.Parameters.Add("O_RECS", OracleDbType.RefCursor, null, ParameterDirection.Output);
like image 21
moke Avatar answered Jan 18 '23 03:01

moke