Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Random exceptions when calling UWP APIs on .NET 5 #640

Closed
jkotas opened this issue Dec 16, 2020 · 11 comments · Fixed by #674
Closed

Random exceptions when calling UWP APIs on .NET 5 #640

jkotas opened this issue Dec 16, 2020 · 11 comments · Fixed by #674
Assignees
Labels
bug Something isn't working fixed Issue has been fixed in an upcoming or existing release
Milestone

Comments

@jkotas
Copy link
Member

jkotas commented Dec 16, 2020

Original author @a11cf0 . Transferred from dotnet/runtime#46148

Description

I'm trying to call Windows 10 UWP OCR APIs from a .NET 5 Windows Forms application. The code which i'm using was perfectly working on .NET Core 3.1 but on .NET 5 it's extremely unstable. For some reason the code throws either ObjectDisposedException or sometimes InvalidCastException. this usually happens on the call to GetSoftwareBitmapAsync() but sometimes right on the first await. Also, hen wrapped in a method, it usually works on the first run but on subsequent runs it fails as above. Here's a full code sample tested on both .NET Core 3.1 and .NET 5 with the above results.

using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Threading.Tasks;

namespace Supergovno
{
    static class Program
    {
        static async Task<int> Main()
        {
            Bitmap Screen = new Bitmap(500, 500);
            Graphics g = Graphics.FromImage(Screen);
            g.CopyFromScreen(1, 1, 00, 0, Screen.Size);
            Bitmap S = new Bitmap(Screen, new Size(500, 500));
            var engine = Windows.Media.Ocr.OcrEngine.TryCreateFromLanguage(new Windows.Globalization.Language("en"));
            using MemoryStream memoryStream = new();
            S.Save(memoryStream, ImageFormat.Bmp);
            var decoder = await Windows.Graphics.Imaging.BitmapDecoder.CreateAsync(memoryStream.AsRandomAccessStream());
            var softwareBitmap = await decoder.GetSoftwareBitmapAsync();
            var ocrResult = await engine.RecognizeAsync(softwareBitmap);
            Console.WriteLine(ocrResult.Text);
            Console.WriteLine("OK!");
            Console.ReadLine();
            return 0;
        }
    }
}

Configuration

I'm using .NET 5.0.101 on Windows 10 X64 10.0.19042.

Regression?

Yes. Everything is working on .NET Core 3.1.

Other information

@manodasanW
Copy link
Member

ObjectDisposed exception is probably a dupe of #584. @a11cf0 Can you provide the exception message or stack when you got the InvalidCastException,

@a11cf0
Copy link

a11cf0 commented Dec 16, 2020

ObjectDisposed exception is probably a dupe of #584. @a11cf0 Can you provide the exception message or stack when you got the InvalidCastException,

System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation.
---> System.InvalidCastException: Specified cast is not valid.
at MagicKeys.MagicKeys.OCR(Int32 W, Int32 H, Int32 X, Int32 Y, Int32 Zoom) in D:\Root\src\MagicKeys\MagicKeys\Core\ImgManager\ImgToText.cs:line 42
at MagicKeys.MagicKeys.<>c__DisplayClass63_0.<b__0>d.MoveNext() in D:\Root\src\MagicKeys\MagicKeys\Core\ImgManager\ImgToText.cs:line 22
--- End of stack trace from previous location ---
at MagicKeys.MagicKeys.ImgToText(Int32 W, Int32 H, Int32 X, Int32 Y, Int32 Zoom) in D:\Root\src\MagicKeys\MagicKeys\Core\ImgManager\ImgToText.cs:line 22
at MagicKeys.MagicKeys.VUFOCR(String Param) in D:\Root\src\MagicKeys\MagicKeys\Core\VUFManager\VUFOCR.cs:line 11
--- End of inner exception stack trace ---
at System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor, Boolean wrapExceptions)
at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
at System.Reflection.MethodBase.Invoke(Object obj, Object[] parameters)
at MagicKeys.MagicKeys.InvokeFromString(String InvokeFunc, String FuncParam) in D:\Root\src\MagicKeys\MagicKeys\Core\Common\InvokeFromString.cs:line 33

@a11cf0
Copy link

a11cf0 commented Dec 16, 2020

I see that #584 has been fixet. Is it possible to somehow apply this fix without switching to experimental .NET revisions or waiting for .NET 6?

@manodasanW
Copy link
Member

@a11cf0 , typically once we check in a fix, it will be shipped as part of the next CsWinRT update which will then be used to generate the Windows SDK projection packages and shipped in a .NET 5 update. So your probably won't need to wait till .NET 6 unless the fix is considered a breaking change.

But it looks like I am still able to hit the ObjectDisposed exception in your repro even with the fix from the bug I mentioned. We will need to investigate further, but your repro is helpful.

@huoyaoyuan
Copy link
Contributor

My call stack is different, but it's an ObjectDisposed when accessing ImageSource.SetSourceAsync. Maybe related.

@huoyaoyuan
Copy link
Contributor

I can still hit a random ObjectDisposedException after updating to .NET SDK 5.0.102

@huoyaoyuan
Copy link
Contributor

huoyaoyuan commented Jan 20, 2021

@manodasanW I've tried your fix (copying WinRT.Runtime.dll from latest master), but my app is still crashing.

Yes confirmed. My issue is still present.

Since the exception shows as a native exception and can't be caught at managed side, I think it's probably a WinUI issue instead.

@a11cf0
Copy link

a11cf0 commented Feb 13, 2021

On .NET 5.0.103 with almost the same test code I no longer get the ObjectDisposedException but the InvalidCastException is now extremely frequent. Previously it was quite rare.

System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation.
 ---> System.InvalidCastException: Unable to cast object of type 'Windows.Foundation.AsyncOperationWithProgressCompletedHandler`2[Windows.Storage.Streams.IBuffer,System.UInt32]' to type 'Windows.Foundation.IAsyncOperation`1[Windows.Graphics.Imaging.BitmapDecoder]'.
   at MagicKeys.MagicKeys.ImgToText(Int32 W, Int32 H, Int32 X, Int32 Y, Int32 Zoom) in D:\Root\src\MagicKeys\MagicKeys\Core\ImgManager\ImgToText.cs:line 27
   at MagicKeys.MagicKeys.VUFOCR(String Param) in D:\Root\src\MagicKeys\MagicKeys\Core\VUFManager\VUFOCR.cs:line 11
   --- End of inner exception stack trace ---
   at System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor, Boolean wrapExceptions)
   at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
   at System.Reflection.MethodBase.Invoke(Object obj, Object[] parameters)
   at MagicKeys.MagicKeys.InvokeFromString(String InvokeFunc, String FuncParam) in D:\Root\src\MagicKeys\MagicKeys\Core\Common\InvokeFromString.cs:line 33

@manodasanW
Copy link
Member

@a11cf0 Are you running into the InvalidCastException with the above repro code that you had posted?

@a11cf0
Copy link

a11cf0 commented Feb 14, 2021

@a11cf0 Are you running into the InvalidCastException with the above repro code that you had posted?

No, but the main part is the same. Here's a complete code sample that throws the exception. I added the loop, because the InvalidCastException is never thrown on the first run.

using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace govno
{
    static class Program
    {
        static async Task Main()
        {
            for (int i = 0; i < 10; i++)
            {
                Bitmap Screen = new Bitmap(500, 500);
                Graphics g = Graphics.FromImage(Screen);
                g.CopyFromScreen(1, 1, 00, 0, Screen.Size);
                Bitmap S = new Bitmap(Screen, new Size(500, 500));
                var engine = Windows.Media.Ocr.OcrEngine.TryCreateFromLanguage(new Windows.Globalization.Language("en"));
                using MemoryStream memoryStream = new();
                S.Save(memoryStream, ImageFormat.Bmp);
                var decoder = await Windows.Graphics.Imaging.BitmapDecoder.CreateAsync(memoryStream.AsRandomAccessStream());
                var softwareBitmap = await decoder.GetSoftwareBitmapAsync();
                var ocrResult = await engine.RecognizeAsync(softwareBitmap);
                MessageBox.Show(ocrResult.Text, "Result");
            }
        }
    }
}

@manodasanW
Copy link
Member

Remaining issue mentioned here is tracked by #762

@angelazhangmsft angelazhangmsft added the fixed Issue has been fixed in an upcoming or existing release label May 13, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working fixed Issue has been fixed in an upcoming or existing release
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants