tryphotino / photino.NET

https://tryphotino.io
Apache License 2.0
833 stars 68 forks source link

ShowMessage throws AccessViolationException #141

Closed LauraWebdev closed 1 year ago

LauraWebdev commented 1 year ago

Hey there, I've just updated to 2.4.0 and wanted to check out the new native dialogs. When trying to test out ShowMessage, I receive an AccessViolationException.

        var window = new PhotinoWindow().SetLogVerbosity(2)
            .SetTitle("New Window")
            .SetSize(1100, 750)
            .SetUseOsDefaultSize(false)
            .Center()
            .SetResizable(true)
            // LINUX FIXME: https://github.com/tryphotino/photino.NET/issues/83#issuecomment-1554395461
            .RegisterSizeChangedHandler((sender, size) =>
            {
                var senderWindow = (PhotinoWindow?)sender;

                if (size.Width < 800) senderWindow?.SetWidth(800);
                if (size.Height < 650) senderWindow?.SetHeight(650);
            })
            .RegisterWebMessageReceivedHandler(messageHandler.RegisterWebMessageReceivedHandler);

And then later on:

window.ShowMessage("Test Title", "Test Text", PhotinoDialogButtons.Ok, PhotinoDialogIcon.Error);

The full exception is the following:

System.AccessViolationException: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.

I can confirm that window holds a valid PhotinoWindow at this point. I am running this on the latest release Windows 11. I have not loaded a URL into the window at that point since I want to use the native dialog to show an error message when it would prevent the application from running.

philippjbauer commented 1 year ago

I'm not able to recreate this on a fresh install of Windows 11 (latest version, all updates applied). Is this perhaps related to your messageHandler.RegisterWebMessageReceivedHandler?

I tested with a the .NET sample from the sample project that I updated to Photino 2.4.0:

https://github.com/tryphotino/photino.NET/assets/3707196/19ce3cf7-e106-48f3-a18c-b3aacdb872e5

LauraWebdev commented 1 year ago

Is it required to be used within the Handler? I've tried to call it on the created window object after initialization.

I wanted to display an error message before calling window.load. See: https://github.com/LauraWebdev/client-next/blob/main/SpinShareClient/Program.cs#L31 (I've previously had this instance lock on Line 60 after initializing window)

LauraWebdev commented 1 year ago

I can confirm that ShowOpenFolder does work within the MessageHandler as used in https://github.com/LauraWebdev/client-next/blob/main/SpinShareClient/MessageParser/CommandLibrarySelectPath.cs (Which is just my abstraction layer for communication but is run within the handler.)

I'm running on a fairly new Win11 22621.1848 build.

philippjbauer commented 1 year ago

Yes, since it needs to be called after the window.WaitForClose() method was called which initialized the native window object and loads the window into memory. Before that the reference will not be able to point to a correct location in memory.

philippjbauer commented 1 year ago

You're trying to prevent the user opening multiple instances of the application if I read everything correct?

LauraWebdev commented 1 year ago

Yes that's the idea. I want to give visual feedback that there can't be more than one instance open.

philippjbauer commented 1 year ago

I added a ticket to the native project with that functionality as an enhancement since that would be the only place I can currently think of where we can open a native dialog without having to reference the window. For now you should be able to get away with sending a message after the page loads and close the second instance programmatically.

https://github.com/tryphotino/photino.Native/issues/111