Unit Tests - Attempted to read or write protected memory - System.AccessViolation

Apr 23, 2015 at 10:14 AM
Edited Jun 25, 2015 at 12:21 PM
Hi,

I have been using Effort framework of just over 3 weeks now. Its seems to work great and has helped greatly in TDD and EF.

Yesterday tried to run the Unit Tests on another colleagues machine, however all the tests (some just didn't run) received the following Exception:
System.AccessViolationException

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

[Stack Trace]
   at DataRow1..ctor(Nullable`1 )
   at lambda_method(Closure , Nullable`1 )
   at System.Linq.Enumerable.WhereSelectListIterator`2.MoveNext()
   at NMemory.Execution.CommandExecutor.ExecuteQuery[T](IExecutionPlan`1 plan, IExecutionContext context, ITable[] tablesToLock, Boolean cloneEntities)
   at NMemory.Execution.CommandExecutor.ExecuteQuery[T](IExecutionPlan`1 plan, IExecutionContext context)
   at NMemory.Linq.TableQuery`1.GetEnumerator(IDictionary`2 parameters, Transaction transaction)
   at NMemory.Linq.TableQueryWrapper`1.GetEnumerator()
   at NMemory.Linq.TableQueryWrapper`1.System.Collections.IEnumerable.GetEnumerator()
   at Effort.Provider.EffortDataReader..ctor(IEnumerable result, Int32 recordsAffected, FieldDescription[] fields, DbContainer container)
   at Effort.Internal.CommandActions.QueryCommandAction.ExecuteDataReader(ActionContext context)
   at Effort.Provider.EffortEntityCommand.ExecuteDbDataReader(CommandBehavior behavior)
   at System.Data.Common.DbCommand.ExecuteReader(CommandBehavior behavior)
   at System.Data.Entity.Infrastructure.Interception.DbCommandDispatcher.<>c__DisplayClassb.<Reader>b__8()
   at System.Data.Entity.Infrastructure.Interception.InternalDispatcher`1.Dispatch[TInterceptionContext,TResult](Func`1 operation, TInterceptionContext interceptionContext, Action`1 executing, Action`1 executed)
   at System.Data.Entity.Infrastructure.Interception.DbCommandDispatcher.Reader(DbCommand command, DbCommandInterceptionContext interceptionContext)
   at System.Data.Entity.Internal.InterceptableDbCommand.ExecuteDbDataReader(CommandBehavior behavior)
   at System.Data.Common.DbCommand.ExecuteReader(CommandBehavior behavior)
   at System.Data.Entity.Core.EntityClient.Internal.EntityCommandDefinition.ExecuteStoreCommands(EntityCommand entityCommand, CommandBehavior behavior)
   at System.Data.Entity.Core.Objects.Internal.ObjectQueryExecutionPlan.Execute[TResultType](ObjectContext context, ObjectParameterCollection parameterValues)
   at System.Data.Entity.Core.Objects.ObjectQuery`1.<>c__DisplayClassb.<GetResults>b__a()
   at System.Data.Entity.Core.Objects.ObjectContext.ExecuteInTransaction[T](Func`1 func, IDbExecutionStrategy executionStrategy, Boolean startLocalTransaction, Boolean releaseConnectionOnSuccess)
   at System.Data.Entity.Core.Objects.ObjectQuery`1.<>c__DisplayClassb.<GetResults>b__9()
   at System.Data.Entity.Infrastructure.DefaultExecutionStrategy.Execute[TResult](Func`1 operation)
   at System.Data.Entity.Core.Objects.ObjectQuery`1.GetResults(Nullable`1 forMergeOption)
   at System.Data.Entity.Core.Objects.ObjectQuery`1.<System.Collections.Generic.IEnumerable<T>.GetEnumerator>b__0()
   at System.Lazy`1.CreateValue()
   at System.Lazy`1.LazyInitValue()
   at System.Lazy`1.get_Value()
   at System.Data.Entity.Internal.LazyEnumerator`1.MoveNext()
   at System.Linq.Enumerable.Single[TSource](IEnumerable`1 source)
   at System.Data.Entity.Core.Objects.ELinq.ObjectQueryProvider.<GetElementFunction>b__3[TResult](IEnumerable`1 sequence)
   at System.Data.Entity.Core.Objects.ELinq.ObjectQueryProvider.ExecuteSingle[TResult](IEnumerable`1 query, Expression queryRoot)
   at System.Data.Entity.Core.Objects.ELinq.ObjectQueryProvider.System.Linq.IQueryProvider.Execute[TResult](Expression expression)
   at System.Data.Entity.Internal.Linq.DbQueryProvider.Execute[TResult](Expression expression)
   at System.Linq.Queryable.Any[TSource](IQueryable`1 source, Expression`1 predicate)
So to try and solve this (and blame the Laptop) I had another colleague run the unit test on his machine. Success. Everything ran fine.

Finally I had another colleague run the same unit test on his machine....failed due to the same error as above.

I have checked if all three had Effort.EF6 installed and NMemory (and of course Entity Framework) and they all did.

Any ideas?

Thanks
May 27, 2015 at 2:53 PM
Hello,

I'm seeing the exact issue hickscse described on my laptop, but my desktop is not having the same issue.

Very strange.

Thanks
Jun 25, 2015 at 12:20 PM
If anyone has any ideas about this please let us know.

Just an update I have tried the following - Tools menu ->Options -> Debugging -> General -> Uncheck this option "Suppress JIT optimization on module load" which was found https://social.msdn.microsoft.com/Forums/en-US/4db3a7e7-0918-40e3-be18-88a8afcc247d/attempted-to-read-or-write-protected-memory-this-is-often-an-indication-that-other-memory-is?forum=adodotnetdataproviders

Not the same scenario. This did not work unfortunately!
May 14, 2016 at 2:20 PM
We recently started playing around with Effort hoping to remove our database dependencies from our unit tests, and we've just ran into this same error. For us it appears to be triggered when we have Telerik's JustMock profiler enabled. I believe it uses the .NET Profiler API to allow for more advanced mocking functions (static/concrete members, etc) , and unfortunately we have some tests that require it right now.

Did you guys ever find a solution? We're at a loss for how to make the two play nicely together - short of removing our dependence on the profiler functionality.