last day (14 days later) » 

1:50 PM
0
A: How to force an application crash when AccessViolationException is detected

Chris O'NeillTry this: this.Dispatcher.BeginInvoke((Action) delegate { User32.SendMessage(...); }, DispatcherPriority.Input); The application should crash the way you want it to. Why does this happen? It seems that the CLR code, way down in: at System.RuntimeMethodHandle.InvokeMethod(Object targe...

 
I did not expect this do work. I made the change anyway, and it does indeed appear to make a difference (I will continue verifying this before I accept the answer though, need to be sure this works). Would you mind explaining why there would be any difference between a delegate and Action? I don't see why it would behave differently.
 
I've edited the answer to explain my findings. Note that the suggested code snippet is slightly different.
 
I don't think this explanation is correct, the ThreadStart variant actually works perfectly fine (as long as the unmanaged part doesn't crash). So there must be some difference handling Action and ThreadStart internally, I find this hard to believe...
 
"the ThreadStart variant actually works perfectly fine (as long as the unmanaged part doesn't crash)". So ... it doesn't work perfectly fine? Don't be confused into thinking that ThreadStart must be the right way to call BeginInvoke just because it works most of the time. I've added some references for calling BeginInvoke. Feel free to go poking around in the CLR code to see what causes this behavior. I have and I could spend a lot of time finding the code that actually runs when System.RuntimeMethodHandle.InvokeMethod() is called, to get you a 'water tight' explanation.
Added an example showing that ThreadStart isn't the only delegate that only half works.
 
"just because it works most of the time." ThreadStart works all of the time. Action also works all of the time. Whether or not it fails depends on the unmanaged code (which doesn't care about the method used), the only (and surprising) difference is how these two variants fail. So you can't say that ThreadStart is the wrong way. Maybe I'll find a reason somehwhere...
 
1:50 PM
ThreadStart does only work most of the time. In your example you need it to do two things. 1. Call the code in the delegate and 2. pass on any exceptions inside the delegate to the application and crash handler. It does 1 but not 2. Unmanaged exceptions only happen sometimes so ThreadStart 'works' most of the time.
 
"2. pass on any exceptions inside the delegate to the application and crash handler." I don't actually need to do that at all because I don't in both cases.
so I am still not getting your point -_-
I also made an IL code comparison, the only difference in the wole method is
L_0019: newobj instance void [mscorlib]System.Threading.ThreadStart::.ctor(object, native int)
VS
L_0019: newobj instance void [mscorlib]System.Action::.ctor(object, native int)
 
sorry. I don't understand either. You do want it to pass on the real exception so the application crashes. That's the point of the original question.
 
no, not at all ;)
 
Ok, seems I've missed the point. Explain.
 
I am not passing any exceptions to anyone. When we are talking abotu exceptions, I assume we talk about "managed" exceptions.
so the crash handler we use doesn't care about managed exceptions. it only kicks in if the process gets terminated unexpectedly
which is the case if C++ accesses a NULL pointer for example
the application is gone if that happens. except: if this fault happens within a BeginInvoke() call, .NET actually makes a (managed!) AccessViolationException out of that fault, wraps it inside a TargetInvocationException and then eventually results in process termination
 
1:57 PM
ok. yes I get you now. Isn't that a hardware exception?
 
outside the BeginInvoke call, no (managed!) AccessViolationException is generated.
I don#t think this is a hardware exception, rather something the OS does for you
so the difference can only be a result of some type checking within the .NET code (maybe it wraps exceptions from ThreadStart delegates and not Actions?), but I didn't find any such piece of code in the framework yet
 
so without BeginInvoke the code produces an unmanaged AccessViolationException
 
maybe that's part of some not yet published code
it produces an unmanaged access violation in both cases
 
does the version I sent with Action, do what you need it to do. i.e. give you a correct stack trace in the crash dump?
 
it's just that one makes a managed exception out of it and the other does not
yes, Action produces a better stack trace
but both are delegate types (same signature in fact) so there is no technical reason for that
to me it seems like you just made a very good guess ;)
(which is equally helpful of course, but I like to understand the reasons too)
check this piece of code for example
 
2:03 PM
I'll admit that, I did try some of the other method signatures BeginInvoke and Action worked when I tested it. Initially I thought it was the explicit Action extension methods behaving differently. But I started digging down into the Framework and CLR when you asked for an explanation.
 
the code I linked to catches exceptions rewraps it inside a managed exception
this would lead to a not very helpful stack trace if it causes the process termination to be delayed
but I couldn't find any similar piece of framework code that would do that for Dispatcher.BeginInvoke calls
what did you actually test btw? did you really simulate an app crash?
 
I don't think that looking for the reason in IL code would turn up anything. Action and ThreadStart are both delegates as you said, so no difference. It might be that System.RuntimeMethodHandle.InvokeMethod is casting the target to an Action when an exception occurs (for some reason). When the target is not Action you get the TargetInvocationException
 
but then wouldn't it fail even if the unmanaged code works fine?
 
not if it just calls the delegate on the 'happy path'. i.e. it doesn't cast it just calls a delegate. When something goes wrong (exception) it then does a cast and tries to access something(?) on the Action.
 
hm...sounds weird. but without the corresponding source code we won#t be able to tell
 
2:09 PM
Yes. That's what I was getting at.
 
OK, just for my information: how did you test the different hebaviour in the first place?
 
I just make the assumption that your working code (without BeginInvoke) was throwing a managed AccessViolationException. So I made a simple WPF app with the following code:
private void Button_Click(object sender, RoutedEventArgs e)
{

Dispatcher.BeginInvoke((Action) delegate
{
// Send message to unmanaged control for performing a specific task.
throw new AccessViolationException("test");
}, DispatcherPriority.Input);

}
Throws TargetInvocationException
no scratch than throws AccessViolationException
but this:
    private void Button_Click_1(object sender, RoutedEventArgs e)
    {
        this.Dispatcher.BeginInvoke(DispatcherPriority.Input, (ThreadStart) delegate
        {
            throw new AccessViolationException("test");
        });
    }
Throws TargetInvocationException
 
"no scratch than throws AccessViolationException" - what do you mean?
 
I got the code snippets the wrong way around. The top one is correct and throws AccessViolationException. The second one simulates your example, only without the unmanaged component.
This is as far down into the CLR before I hit a brick wall:
 
hm OK. I will use these snippets for testing too, maybe find a difference in execution path somehwhere
thanks for your help so far :)
 
2:19 PM
I don't know how to find the code that runs when
FCDECL4(Object*, InvokeMethod, Object *target, PTRArray *objs, SignatureNative* pSig, CLR_BOOL fConstructor)
is resolved to an actual method.
 
maybe I can make another question about that and post one of the MS forums, we'll see.
having a solution for now is what matters most right now
 
exactly.
keep me posted and let me know if you can get anywhere with the CLR C++
 
will award you with the points within the next couple of days ;)
yes, will do
have a nice day :)
 
Many Thanks. What is your link to cdburnerxp.se?
 
just one of the applications I work on, feel free to check
 
2:22 PM
author / maintainer?
 
yep
 
I love the app. I've used it for years.
 
haha, good no know, thanks :)
*to know
anyway, need to cointue doing some work now, bye!
 
ok. me too. :-) bye
 

last day (14 days later) »