38

I've been trying to find out more about this problem and I'm not having much luck. I keep reading that applications should not have this error come up and although that's all fine and dandy, it doesn't tell me what can cause this error to show up.

I know this question is very broad as I'm sure there can be multiple causes for this error so I'll try to narrow it down a bit.

I'm working in VS2003 developing an application that uses C++.NET

The application uses mostly unmanaged code and little managed code (due to heavy interference by the garbage collector). So I'd rate it 95% unmanaged, 5% managed

I've read somewhere that unstable/buggy/incorrect unmanaged code can mess up parts of the CLR memory rendering it corrupt and throwing this error.

Since 95% of the application is unmanaged, I'm not sure where to start looking. Maybe the few classes that interact between managed and unmanaged? What about marshalling data from managed to unmanaged? Can a bad null pointer cause this failure? What other problems can cause this? Array Index out of bounds? What about a Null Object?

Any information/paper/article that can give a nice list of possible causes for the System.ExecutionEngine failure would be appreciated!


Based on answers this exception can be caused in multiple scenarios, mentioning them here in the question for better visibility.

List of possible causes/scenarios:

Possible workarounds/solutions:

28 Answers 28

8

This should not be taken too seriously, but I managed to get this exception while having some fun with reflection:

typeof(IntPtr).GetField("Zero").SetValue(null, new IntPtr(666));
0
5

Had the same problem - my bug turned out to be an argument mismatch between my C# code and my C++ native dll. I had added an argument to the C++ function and failed to add it to the C# side.

4

There is a known bug when a WCF service tries to return an IList or IEnumerable.

http://connect.microsoft.com/wcf/feedback/details/433569/wcf-throws-an-executionengineexception-when-returning-an-array-as-ilist-t

1
  • 2
    This link is dead: The system has encountered an unexpected error! Jun 26, 2013 at 19:06
4

I encountered it when calling a function in an unmanaged library when my managed code was compiled for release. The error goes away when compiled for debugging.

0
4

Quoted from the MSDN reference page for System.ExecutionEngineException:

The exception that is thrown when there is an internal error in the execution engine of the common language runtime. This class cannot be inherited.

There is no non-obsolete alternative to ExecutionEngineException. If further execution of your application cannot be sustained, use the FailFast method.

Tip

In some cases, an application that targets the .NET Framework may throw an ExecutionEngineException exception during garbage collection when an application or the system on which it is running is under a heavy load. In this case, To work around this issue, you can disable concurrent garbage collection by modifying the application's configuration file. For more information, see How to: Disable Concurrent Garbage Collection.

0
2

Honestly, the only time I've ever seen this exception is when I was using the Compuware DevPartner tools to do some profiling and code analysis. DevPartner hooks deep into the core of the CLR to do it's work, but it's full of bugs, so screws the CLR up. I had to reboot my machine (and remember to never click the DevPartner toolbar buttons ever again) to get things to go back to normal.

If you're not using devpartner, then you most likely have some unmanaged code which is trashing some memory used by the CLR. I'd advise first doing a reboot, and then trying to track down whichever bug you have which is trashing the memory. Look for buffer overruns, writing to uninitialised pointers, and all the other usual suspects.

1
  • Thank you, I wonder if there is a decent unmanaged/managed c++.NET monitoring software that you know about to help me? The application is heavily multi-threaded and asynchronous in 3 different ways. When it runs, it holds a little over 1000 threads so the task of tracking a run-time error is immense... Thank you for your answer, I appreciate the time you took and I will continue to look into this issue. I hope more people can join and give more advice! Alex.
    – Alex
    Jun 9, 2009 at 16:26
2

I am getting this ExecutionEngineException when just using standard .NET Clipboard calls. My code is 100% managed. So, it seems the .NET Framework has issues.

PastedData = Clipboard.GetDataObject();
object imageObj = PastedData.GetData(dataType);

where data type is "EnhancedMetafile", which was in the list of formats on the Clipboard.

2

update your application to .net 4.0 OR 4.5 because that exception is obsolete. "Note: This API is now obsolete." see: http://msdn.microsoft.com/en-us/library/system.executionengineexception(v=vs.100).aspx

6
  • 2
    This actually fixed it for me. I was getting this exception when trying to run my unit tests. Upgrading the unit test project to 4.5 fixed it.
    – tster
    Jul 24, 2013 at 0:06
  • 1
    Under .net 4.0 this error still occurs for me. When I run the solution under Visual Studio then at some moment the process closes unexpectedly and in the output window I can see log message telling aboyt System.ExecutionEngineException. So it seems it may be "obsolete" for the framework users, but not for the .NET system itself... Anybody can comment on this? Jan 9, 2014 at 5:59
  • 1
    Under .Net 4.0. it is still happening, I'm debugging it now.
    – Matt
    Jan 23, 2015 at 15:08
  • under 4.6 to, though it may be related to corrupted DLL from the EF nuget package
    – MikeT
    May 31, 2016 at 11:49
  • @MikeT What EF package? Can you provide more info? I seem to also get the exception in the EF context.
    – Monsignor
    Mar 6, 2019 at 15:02
2

I recently discovered that using the std instruction in assembler to set the direction flag without clearing it when you're done (cld) can cause this error. I'm using .Net 4.0.

2

I've come across this exception while developing a ArcGIS extension, written with VB.NET (Visual Studio 2005). ArcGIS relies heavily on the COM technology, therefore .NET-COM interop is involved. I haven't found the cause of the exception until now, but I suppose it might have to do with a building-up of un-released COM object instances, since the exception only ever occurs after the software has been crunching geometries and numbers for some time.

1

I've just come across this exception while working on WPF application. VS showed that it happened while notifying property change through NotifyPropertyChanged call(INotifyPropertyChanged).

Looks very strange to me and it happened only once; it stopped the debugging session and didn't happened after running the application again.

1
  • Could be related to setting a property from a non-UI thread. Note, the App class is running in a non-UI thread.
    – Mike
    Nov 15, 2020 at 9:52
1

One more case when System.ExecutionEngineException is thrown is when an unhandled exception is thrown in delegate provided to ThreadPool.QueueUserWorkItem() function.

0

I got one of these when my C# module (invoked by a C++/MFC app built with /CLR) inadvertently de-referenced a null pointer. So it is possible for the exception to occur due to a bug in the "user code".

Kevin

0

I had this exception when running unmanaged code in a different version, than I compiled it with.

Maybe that helps someone...

0

I've just come across this exception while writing a c# programme using the Unity Framework.

I'm using VS 2010, and .NET 3.5

Basically if you register a type in a UnityContainer:

container.RegisterType<IAClass, AClass>();

but AClass doesn't have a constructor that takes no arguments

class AClass : IAClass
{
   private int a;
   public in A { get { return a; } }
   public AClass(int a)
   {
       this.a = a;
   }
}

then when you come to instantiate the class you'll get the System.ExecutionEngineException

IAClass A = container.Resolve<IAClass>(); //throws System.ExecutionEngineException
1
  • I'm currently getting this exception with nHibernate and StructureMap but I have not been able to pinpoint the origin.. thanks for ideas on this Nov 16, 2011 at 19:02
0

I get this Exception when viewing pdf files with an external viewer. I think the viewer itself is the root of all evil. It has to be something outside of the .NET framework obviously.

0

Using the wrong marshaling causes me this exception. I've had a pure C *.dll to use; I write a wrapper around it in C#, using DllImport; then I used marshaling to let out string parameters to fill a StringBuilder, like this:

    [DllImport("old.dll", CallingConvention = CallingConvention.Winapi, CharSet = CharSet.Ansi)]
    public static extern UInt32 GetMessage([MarshalAs(UnmanagedType.LPStr)] out StringBuilder message);

Using UnmanagedType.LPStr causes me that exception; using UnmanagedType.BStr, the right string marshal in this case, solved me the problem. Just my 2 cent. :-)

1
  • 1
    same for me - in my case use use <UnmanagedType.BStr> instead of <UnmanagedType.LPWStr>. thanx ferdinando :)
    – yarg
    Jan 31, 2019 at 17:39
0

I managed to get this exception while debugging a C# 4.0 web application which uses an assembly that uses an Azure service bus. This assembly has a bug where it will try to loop forever to receive a message from a closed servicebus, but the exception handler captures that exception. However, while debugging the code, it actually killed Visual Studio 2012 with this exception! Yes, VS2012 was the one that threw the exception.

0

Just to add to the list of possible causes, I've had this error when trying to serialise IEnumerable<> containing a complex type.

Changing from IEnumerable<> to List<> resolved the problem.

1
  • Similar problem here - A service call which returns an IList<> would throw the error because it was returning an instance of Array instead of an instance of List<> Jan 12, 2010 at 13:22
0

I had this happen and it was because I was calling ::FreeLibrary() multiple times with the same DLL HANDLE. (The DLL was a managed C++ dll: a managed C++ wrapper over some C# functionality)

0

I'll add how I hit this.

Using .Net 4 code contacts, a Contract.Ensures(Contract.Result<object>() != null) will throw this exception IIF you have Assert on Contract Failure check in the project properties page for Code Contracts. Disabling this checkbox doesn't disable the check however. You get the expected "Post condition failed message."

0

I got this error when I moved a harddisk from one computer to another (win 8 reinstalled drivers automatically ) but I suspect the native gened exes will not work.

0

Getting this problem in pure managed WPF app in a pretty innocent situation it seems to me. I have a control in a grid which I want to display or hide so I'm setting it's visibility to Visibility.Collapsed or Visibility.Visible. It starts out collapsed. I press a button and it gets set to visible and appears just fine. I press another button and set it to collapsed and boom - big ugly error. Nothing fancy or all that unusual. This is using 4.5. Very odd. When I look at the data for the error it says System.Collections.EmptyReadOnlyDictionaryInternal.

0

I get this error in a regular LINQ Query where I am returning FirstOrDefault using VS2010

0

Using VS2015 ASP.NET Framework 4.5 I just had to rebuild and run again.

0
  1. Create new Web Forms Application

  2. Open any page (lets say Default.aspx)

  3. Add GridView to the form

  4. Click on the GridView, then on the little arrow (upper right corner). When I click "Edit columns" or "Add new column" or Data Source > New Data Source, Visual Studio 2015 crashes with System.ExecutionEngineException in mscorlib.dll

0

I am getting this exception in a .NET Framework 3.5 WPF application.

The exception occurs whenever the Close method is called in the Loaded window event. This occurs in a barebones window, as well.

However, the exception only occurs if I set the property SizeToContent="WidthAndHeight" in the xaml of the view. Removing this property fixes the issue.

I was also using the MVVM Light library. However, the exception occurs even after removing references to this library for this WPF window.

0

On my project I just discovered that using obfuscated DLLs can be a cause of this.

Here is the setup which caused the problem:

  • Visual Studio startup project produces a C# DLL. It is set to run an external program which will load said DLL + some others.

  • The external program is located under Program Files along with various .NET DLLs from our last build.

  • The build includes obfuscating DLLs (using ConfuserEX, though maybe any other obfuscation tool would cause the same problem if it obfuscated in the same way).

  • At runtime we would immediately get an ExecutionEngineException (preceded by a "Managed Debugging Assistant 'FatalExecutionEngineError' has detected a problem..." message).

  • At that point looking in the Visual Studio Modules you could see that the DLL noted in the exception was being loaded from Program Files - e.g., it was the obfuscated DLL being loaded.

Replacing any of the obfuscated DLLs with their non-obfuscated equivalents disappeared the exception.

Beats me specifically why obfuscation would cause this exception, but I suspect that the debugger trying to attach is unable to reverse engineer the assembly, much like you can't easily decompile an obfuscated tool by any normal means. This was a case where running with CTRL+F5 (no debugger attached) would NOT have a problem.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.