title: System.AccessViolationException due a System.Data.Objects.ObjectQuery
body: <p>From time to time I'm getting an Exception of the type System.AccessViolationException in my w3wp.exe that crash the app. </p>
  
  <p>The stack of the exception is this (got from the event viewer):</p>
  
  <pre><code>Log Name:      Application
  Source:        .NET Runtime
  Date:          05-04-2013 00:00:27
  Event ID:      1026
  Task Category: None
  Level:         Error
  Keywords:      Classic
  User:          N/A
  Computer:      MYSERVER
  Description:
  Application: w3wp.exe
  Framework Version: v4.0.30319
  Description: The process was terminated due to an unhandled exception.
  Exception Info: System.AccessViolationException
  Stack:
     at System.Collections.Generic.List`1[[System.__Canon, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]..ctor()
     at System.Data.Common.Internal.Materialization.CoordinatorScratchpad..ctor(System.Type)
     at System.Data.Common.Internal.Materialization.Translator.ProcessCollectionColumnMap(System.Data.Query.InternalTrees.CollectionColumnMap, System.Data.Common.Internal.Materialization.TranslatorArg, System.Data.Query.InternalTrees.ColumnMap, System.Object)
     at System.Data.Common.Internal.Materialization.Translator.Visit(System.Data.Query.InternalTrees.SimpleCollectionColumnMap, System.Data.Common.Internal.Materialization.TranslatorArg)
     at System.Data.Query.InternalTrees.SimpleCollectionColumnMap.Accept[[System.__Canon, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089],[System.Data.Common.Internal.Materialization.TranslatorArg, System.Data.Entity, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]](System.Data.Query.InternalTrees.ColumnMapVisitorWithResults`2&lt;System.__Canon,System.Data.Common.Internal.Materialization.TranslatorArg&gt;, System.Data.Common.Internal.Materialization.TranslatorArg)
     at System.Data.Common.Internal.Materialization.Translator.TranslateColumnMap[[System.Boolean, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]](System.Data.Common.QueryCache.QueryCacheManager, System.Data.Query.InternalTrees.ColumnMap, System.Data.Metadata.Edm.MetadataWorkspace, System.Data.Objects.Internal.SpanIndex, System.Data.Objects.MergeOption, Boolean)
     at System.Data.Common.Internal.Materialization.ShaperFactory+TypedShaperFactoryCreator`1[[System.Boolean, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]].TypedCreate(System.Data.Common.QueryCache.QueryCacheManager, System.Data.Query.InternalTrees.ColumnMap, System.Data.Metadata.Edm.MetadataWorkspace, System.Data.Objects.Internal.SpanIndex, System.Data.Objects.MergeOption, Boolean)
     at System.Data.Common.Internal.Materialization.ShaperFactory.Create(System.Type, System.Data.Common.QueryCache.QueryCacheManager, System.Data.Query.InternalTrees.ColumnMap, System.Data.Metadata.Edm.MetadataWorkspace, System.Data.Objects.Internal.SpanIndex, System.Data.Objects.MergeOption, Boolean)
     at System.Data.Objects.Internal.ObjectQueryExecutionPlan.Prepare(System.Data.Objects.ObjectContext, System.Data.Common.CommandTrees.DbQueryCommandTree, System.Type, System.Data.Objects.MergeOption, System.Data.Objects.Span, System.Collections.ObjectModel.ReadOnlyCollection`1&lt;System.Collections.Generic.KeyValuePair`2&lt;System.Data.Objects.ObjectParameter,System.Data.Objects.ELinq.QueryParameterExpression&gt;&gt;)
     at System.Data.Objects.ELinq.ELinqQueryState.GetExecutionPlan(System.Nullable`1&lt;System.Data.Objects.MergeOption&gt;)
     at System.Data.Objects.ObjectQuery`1[[System.Boolean, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]].GetResults(System.Nullable`1&lt;System.Data.Objects.MergeOption&gt;)
     at System.Data.Objects.ObjectQuery`1[[System.Boolean, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]].System.Collections.Generic.IEnumerable&lt;T&gt;.GetEnumerator()
     at System.Linq.Enumerable.Single[[System.Boolean, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]](System.Collections.Generic.IEnumerable`1&lt;Boolean&gt;)
     at System.Data.Objects.ELinq.ObjectQueryProvider.&lt;GetElementFunction&gt;b__3[[System.Boolean, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]](System.Collections.Generic.IEnumerable`1&lt;Boolean&gt;)
     at System.Data.Objects.ELinq.ObjectQueryProvider.ExecuteSingle[[System.Boolean, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]](System.Collections.Generic.IEnumerable`1&lt;Boolean&gt;, System.Linq.Expressions.Expression)
     at System.Data.Objects.ELinq.ObjectQueryProvider.System.Linq.IQueryProvider.Execute[[System.Boolean, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]](System.Linq.Expressions.Expression)
     at System.Linq.Queryable.Any[[System.__Canon, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]](System.Linq.IQueryable`1&lt;System.__Canon&gt;)
     at MY.WebService.NetworkValidation.ValidIp(System.String)
     at MY.WebService.SedeService.IsLogged(MY.DataBase.SedeData.User)
     at MY.WebService.Sede.IsLogged()
     at DynamicClass.SyncInvokeIsLogged(System.Object, System.Object[], System.Object[])
     at System.ServiceModel.Dispatcher.SyncMethodInvoker.Invoke(System.Object, System.Object[], System.Object[] ByRef)
     at System.ServiceModel.Dispatcher.DispatchOperationRuntime.InvokeBegin(System.ServiceModel.Dispatcher.MessageRpc ByRef)
     at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage5(System.ServiceModel.Dispatcher.MessageRpc ByRef)
     at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage41(System.ServiceModel.Dispatcher.MessageRpc ByRef)
     at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage4(System.ServiceModel.Dispatcher.MessageRpc ByRef)
     at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage31(System.ServiceModel.Dispatcher.MessageRpc ByRef)
     at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage3(System.ServiceModel.Dispatcher.MessageRpc ByRef)
     at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage2(System.ServiceModel.Dispatcher.MessageRpc ByRef)
     at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage11(System.ServiceModel.Dispatcher.MessageRpc ByRef)
     at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage1(System.ServiceModel.Dispatcher.MessageRpc ByRef)
     at System.ServiceModel.Dispatcher.MessageRpc.Process(Boolean)
     at System.ServiceModel.Dispatcher.ChannelHandler.DispatchAndReleasePump(System.ServiceModel.Channels.RequestContext, Boolean, System.ServiceModel.OperationContext)
     at System.ServiceModel.Dispatcher.ChannelHandler.HandleRequest(System.ServiceModel.Channels.RequestContext, System.ServiceModel.OperationContext)
     at System.ServiceModel.Dispatcher.ChannelHandler.AsyncMessagePump(System.IAsyncResult)
     at System.ServiceModel.Dispatcher.ChannelHandler.OnContinueAsyncReceive(System.Object)
     at System.Runtime.IOThreadScheduler+ScheduledOverlapped.IOCallback(UInt32, UInt32, System.Threading.NativeOverlapped*)
     at System.Runtime.Fx+IOCompletionThunk.UnhandledExceptionFrame(UInt32, UInt32, System.Threading.NativeOverlapped*)
     at System.Threading._IOCompletionCallback.PerformIOCompletionCallback(UInt32, UInt32, System.Threading.NativeOverlapped*)
  </code></pre>
  
  <p>The code of the function ValidIp(string ip) (last function that I call before get the exception):</p>
  
  <pre><code>try
  {                
      using(var db = new AccessEntities())
      {
          var ret = db.AccessControl.Where(xx =&gt; xx.Ip == ip &amp;&amp; xx.IsValid).Any();                    
          return ret;
      }
  }
  catch (Exception ee)
  {                
      return false;
  }
  </code></pre>
  
  <p>Does anyone have any idea why this is happening ? I call this function every 1h and from times to times give this exception, last time took 2 days and half to happen, another interesting thing is I'm doing a try/catch shouldn't I be able to catch the exception and prevent the w3wp.exe crash ?</p>
  
  <p>Thanks in advance,
  Rui</p>