UWP Windows 10 app crashes in release mode but works fine in debug mode
Asked Answered
C

2

9

My UWP app is crashing in Release mode and works fine in Debug mode but I can't put my finger on what the issue is but I know it's related to a combination of raising an event from System.Threading.Timer and MVVMLight.

I created an new dummy application and use the same code (ZXing.net.mobile where I used 2 portable libraries and I used my own user control which is a simplified version of theirs - I'm using events instead of <Action>). This works fine and I'm currently trying to put more steps into it i.e. include mvvmlight and navigation but so far, I can't reproduce the problem in this dummy app.

The error I'm getting is:

Unhandled exception at 0x58C1AF0B (mrt100_app.dll) in Company.MyApp.App.exe:
0xC0000602:  A fail fast exception occurred. Exception handlers will not be
 invoked and the process will be terminated immediately.

Followed by:

Unhandled exception at 0x0107D201 (SharedLibrary.dll) in
Company.MyApp.App.exe: 0x00001007.

When looking at the Threads window, one of the worker thread has the following information if that's of help.

Not Flagged >   4012    0   Worker Thread   <No Name>
    System.Private.Interop.dll!System.Runtime.InteropServices.
    ExceptionHelpers.ReportUnhandledError   Normal
    [External Code]  
     System.Private.Interop.dll!System.Runtime.InteropServices.ExceptionHelpers.
     ReportUnhandledError(System.Exception e) Line 885   
     System.Private.Interop.dll!Internal.Interop.InteropCallbacks.ReportUnhandledError
     (System.Exception ex) Line 17   
     System.Private.WinRTInterop.CoreLib.dll!Internal.WinRT.Interop.WinRTCallbacks.
     ReportUnhandledError(System.Exception ex) Line 274  
     System.Private.CoreLib.dll!System.RuntimeExceptionHelpers.ReportUnhandledException
     (System.Exception exception) Line 152
     System.Private.Threading.dll!System.Threading.Tasks.AwaitTaskContinuation.
     ThrowAsyncIfNecessary(System.Exception exc) Line 784    
     System.Private.Threading.dll!System.Threading.WinRTSynchronizationContext.Invoker.
     InvokeCore() Line 182   
     System.Private.Threading.dll!System.Threading.WinRTSynchronizationContext.Invoker.
     Invoke(object thisObj) Line 162     
     System.Private.CoreLib.dll!System.Action<System.__Canon>.
     InvokeOpenStaticThunk(System.__Canon obj)
     System.Private.WinRTInterop.CoreLib.dll!Internal.WinRT.Interop.WinRTCallbacks.PostToCoreDispatcher.AnonymousMethod__0() Line 266    
     MyCompany.MyApp.App.exe
     MyCompany.MyApp.App.ViewModels.ValidateHandler.Invoke(string pin)
     MyCompany.MyApp.App.McgInterop.dll!McgInterop.ReverseComSharedStubs
     .Proc_(object __this, System.IntPtr __methodPtr) Line 6163
     MyCompany.MyApp.App.McgInterop.dll!Windows.UI.Core.DispatchedHandler__Impl.Vtbl.Invoke__STUB(System.IntPtr pComThis) Line 45147     
    [External Code]  

Within my QR Code UserControl, it's using a System.Threading.Timer and it raises an event when a QR Code is found:

timerPreview = new Timer(async (state) =>
{
   ....
   // Check if a result was found
   if (result != null && !string.IsNullOrEmpty(result.Text))
   {
     Debug.WriteLine("Barcode Found: " + result.Text);
     if (!this.ContinuousScanning)
     {
       delay = Timeout.Infinite;
       await StopScanningAsync();
     }
     else
     {
       delay = this.ScanningOptions.DelayBetweenContinuousScans;
     }
     OnBarcodeFound(result.Text);
   }

   timerPreview.Change(delay, Timeout.Infinite);

}, null, 
   this.ScanningOptions.InitialDelayBeforeAnalyzingFrames, 
   Timeout.Infinite);

In the page that host the QRCode UserControl, I've got the following code:

private async void ScannerControl_BarcodeFound(string barcodeValue)
{
    var dispatcher = CoreApplication.MainView.CoreWindow.Dispatcher;

    await dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () => {
        Debug.WriteLine(barcodeValue);
        GetViewModel.BarcodeFound(barcodeValue);
    });
}

As you can see, I'm passing the QrCode value to my ViewModel and from there, I sent a message and then re-direct to another page:

public void BarcodeFound(string barcodeData)
{
   Messenger.Default.Send<string>(barcodeData, MessengerTokens.QrCodeFound);
   this.NavigationFacade.NavigateToMyOtherPage();
}

I could keep going and provide additional code, but as I add additional breakpoints I can see that the code and passing the correct value and going to the correct location but eventually it throws this error. If I add additional error handlers or dispatcher code, it eventually works and goes to the next page as expected but when I click on a button in my CommandBar, it then takes a while and it eventually throws the same error, so by adding these additional bits of code, I feel I'm just pushing down the problem further down the line.

Anyone got any suggestions on how I get around this problem. I wish I could share the full app but definitely can't. So I know it will be hard to provide a solution, but if anyone has suggestions I'd appreciate them.

As I said, I think the issue is a result of a combination of threading and mvvmlight but it's driving me nuts that my test app so far is working exactly as expected, so I'm pretty sure it's not Zxing or my UserControl.

Any help, suggestions would be greatly appreciated or if you need me to provide additional info, please let me know what and I'll try to provide it.

Thanks.

Cymry answered 23/4, 2016 at 20:38 Comment(2)
Is it maybe an issue related to .NET Native compiled code used in relase? Have you tried debuging with the "Compile with .NET Native tool chain" enabled?Goldsworthy
Hi, I already have tried that and I've also tried it with the 'Optimize Code' selected, and still doesn't crash while in debug mode.Cymry
C
2

Well, this was a painstaking exercise and a huge waste of my time!

It was down to a few things!!

  1. I was using a different DataService in Releasemode which wasn't implemented. By implemented, I mean all my functions were returning the NotImplementedException or null values.

  2. When passing my model to my ViewModel, I did not check if it was null, thus causing unhandled exceptions.

  3. I had a chain of mvvmlight events (Messenger.Default.Send<>) being triggered and none were checking for error or null values.

While all of these were caused by poor validation from my part, these errors are extremely poorly reported in Release mode! if from the get go, I had received a NullReferenceException or any kind of exceptions, it would have put me in the right direction immediately, but throwing errors such as the one I've had were totally useless but it didn't but lesson learned!!

All I can say is that if this problem ever happens to you, don't waste your time rewriting code or trying to find workarounds. First work your way through your workflow/chain of events and hopefully, you'll eventually catch the culprit.

Hope this helps.

Cymry answered 24/4, 2016 at 2:6 Comment(2)
Hi, my app is having the same problem. The last time ( like 6 month ago), I uninstalled the app, restarted, and installed again and all was fine. So I don't think your or my fix this is a real solution. Now I have the error again, but I don't want to try reinstalling like last time, I want to dig out the error in the first place. Back then, I read that this ddl's error could be caused by plenty issues on system side, failing to read of the dll file. Or- what seems to have been my case, some sort of messed up references in the registry, because I needed access to pictures directory.Aeromechanic
So, did you ever get this problem again, and fixed it another way?Aeromechanic
I
1

Sadly we were facing a similar issue, ours was involved with setting the qualifier values for changing localization on the fly in the app but came up with mystery fail fast/SharedLibrary native errors. Upgrading the Microsoft.NETCore.UniversalWindowPlatform package from 6.0.4 to 6.0.7 seems to have resolved the issue.

Only thought of this because another place I was researching this error involved someone solving a SharedLibrary problem by upgrading their NETCore package, that case was an earlier one (5.x), but figured it was worth a shot.

Ingeringersoll answered 22/2, 2018 at 20:9 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.