Why does this code raise a System.ExecutionEngineException

Background: I use DirectX 9.0 managed libraries to convert arrays of 3D points to 2d screen coordinates. For speed, I use UnsafeNativeMethods to do all the transformations.

Problem: If my custom line trimming function is used, my application dies without any exceptions, it took me a while to realize that it is throwing a fatal System.ExecutionEngineException . I narrowed this down to happen due to the last two lines of my clipping function.

 List<Vector3> verticesAfterClipping = new List<Vector3>; public unsafe void ClipLine(Line lineToClip) { this.verticesAfterClipping.Clear(); // Clipping algorithm happens here... (this is psuedo-code of what it does) foreach(Vertex in lineToClip.Vertices) { bool thisIsClipped = // Set to whether this vertex is clipped bool lastWasClipped = // Set to whether last vertex was clipped if(thisIsClipped == false && lastWasClipped == true) { verticesAfterClipping.Add( /* intersection on clipping plane */ ); verticesAfterClipping.Add( /* thisVertex */ ); } else if (thisIsClipped == false && lastWasClipped == false) { verticesAfterClipping.Add( /* thisVertex */ ); } else if (thisIsClipped == true && lastWasClipped == false) { verticesAfterClipping.Add(/* intersection on clipping plane */); } } // THIS IS WHERE BAD THINGS HAPPEN lineToClip.Vertices = new Vertex[verticesAfterClipping.Count]; verticesAfterClipping.CopyTo(lineToClip.Vertices, 0); } 

When the verticesAfterClipping list is copied to the lineToClip vertices, the lineToClip object lineToClip then passed to UnsafeNativeMethod, which converts these vertices to 2d vertices. From everything that I see, when I go through it in debug mode, it works completely fine until it just dies.

I just can’t understand what’s wrong. Any help would be greatly appreciated.

+7
c # exception managed-directx
source share
2 answers

The line that throws the exception may not cause a problem. This may be a sign of what happened before.

A System.ExecutionEngineException exception is System.ExecutionEngineException when the CLR detects that something went horribly wrong. This can happen some time after the problem occurs. This is because the exception is usually the result of corruption of internal data structures - the CLR detects that something has fallen into a state that does not make sense. It throws an elusive exception because it is unsafe to continue.

Thus, you may have some code in some completely unrelated part of the system that distorts something, but this only becomes apparent when this particular part of the code is executed. The code you showed can be very good. (This may also be wrong ... I don’t see anything obvious wrong, but then I don’t know the well-managed DX 9 libraries. I don’t see which function of this method requires, for example, the unsafe keyword.)

Unfortunately, this means that you need to start casting the network a little wider. Almost everything that uses either unsafe code or COM interoperability is potentially suspicious. Unfortunately, this will be a long and tedious process. One way you can get close to it is to try to simplify the program gradually: what is the smallest piece of code that can illustrate the problem? (For example, if you put the code you showed there in an application that does not contain anything other than the simplest possible call to this method, it still doesn’t work?)

+12
source share

I have the same problem with different libraries. In my case, it all started long before, because I had to run a 32-bit .net application in a 64-bit environment. Well, that causes me a lot of problems, compatibility between architectures or between the CLRs of your .NET platform may be your problem too.

PS: Now I know what my problem is, but I have no idea where it is.

+1
source share

All Articles