maui icon indicating copy to clipboard operation
maui copied to clipboard

Getting AccessViolationException while adding custom resource dictionary in App.Xaml file

Open naveenkumar-sanjeevirayan opened this issue 1 year ago • 5 comments

Description

I have created a Maui library project file to add a custom resource dictionary for theme support. Then, I included it in the application file and added the custom resource dictionary to the merged dictionaries like below. I'm getting AccessViolationException when I run the application in release mode only. And the example deployed successfully with debug mode. I have attached the runnable example with the library project.

<Application xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:local="clr-namespace:CustomThemeTesting"
             xmlns:theme="clr-namespace:ThemeLibrary;assembly=ThemeLibrary"
             x:Class="CustomThemeTesting.App">
    <Application.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="Resources/Styles/Colors.xaml" />
                <ResourceDictionary Source="Resources/Styles/Styles.xaml" />
                <theme:CustomThemeResourceDictionary VisualTheme="LightDefault"/>
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Application.Resources>
</Application>

Error message: AccessViolationException

Steps to Reproduce

  1. Download and extract the attached example.
  2. Run the CustomThemeTesting project with release mode. You will get the access violation exception.

Maui_Theme_Issue.zip

Link to public reproduction project repository

No response

Version with bug

8.0.3

Is this a regression from previous behavior?

Not sure, did not test other versions

Last version that worked well

Unknown/Other

Affected platforms

iOS, Android, Windows, macOS

Affected platform versions

No response

Did you find any workaround?

No response

Relevant log output

No response

@naveenkumar-sanjeevirayan are you seeing this on all 4 platforms? Just want to make sure you made an accurate selection there.

PureWeen avatar Dec 21 '23 19:12 PureWeen

Hi @naveenkumar-sanjeevirayan. We have added the "s/needs-info" label to this issue, which indicates that we have an open question for you before we can take further action. This issue will be closed automatically in 7 days if we do not hear back from you by then - please feel free to re-open it if you come back to this issue after that time.

ghost avatar Dec 21 '23 19:12 ghost

This issue has been automatically marked as stale because it has been marked as requiring author feedback but has not had any activity for 4 days. It will be closed if no further activity occurs within 3 days of this comment. If it is closed, feel free to comment when you are able to provide the additional information and we will re-investigate.

ghost avatar Dec 25 '23 20:12 ghost

Hi, @PureWeen I'm facing this on Windows and Android platforms. I didn't check in Mac and Ios. So, Please ensure from your side for Mac and iOS platforms.

@naveenkumar-sanjeevirayan are you seeing this on all 4 platforms? Just want to make sure you made an accurate selection there.

Hi - I actually reported this to SyncFusion, and they opened the bug.

It failed on iOS, Windows, and Android. I don't use it in MacCatalyst, so I don't know about that one.

If you simply download and run the sample you can see it though.

rlennoxmemoro avatar Jan 03 '24 20:01 rlennoxmemoro

This works fine in .NET7.0 and issue occurring in .NET8.0

PaulAndersonS avatar Jan 17 '24 07:01 PaulAndersonS

@PureWeen Can you check on this

PaulAndersonS avatar Jan 17 '24 07:01 PaulAndersonS

Any update on this?... Please resolve this as soon as possible or any workaround available ? @PureWeen

Ahamed-Ali avatar Jan 23 '24 09:01 Ahamed-Ali

trying to reproduce... do you have a call stack ? what happens if you disable Xaml Compilation on App.xaml ?

StephaneDelcroix avatar Feb 08 '24 15:02 StephaneDelcroix

We found that the IL generated for your custom themes inheriting from CustomeThemeDictionary is wrong. you can disable Xaml compilation on those (by attributing the code behind class with the [XamlCompilation(XamlCompilationOptions.Skip)] attribute as a workaround

StephaneDelcroix avatar Feb 08 '24 16:02 StephaneDelcroix

I was unable to get the AccessViolationException, but there was indeed a NullRefException (that might have caused the AccessViolation one). there's a fix for it in #20463

StephaneDelcroix avatar Feb 09 '24 11:02 StephaneDelcroix

@StephaneDelcroix, We tested the nightly build 8.0.7 but still we face the reported issue. Also, we further checked the test case you have added in your fix PR, and we found that the scenario is different compared to others. We have explained that in the below picture.

image

We have even simplified the replicating sample with the above scenario. In the attached sample We have created a class CustomThemeDictionary : ResourceDictionary. Then another class as LightThemeColors : CustomThemeDictionary. Keys in the LightThemeColors causes the crash whereas if the keys defined directly in CustomThemeDictionary works. Also below is the error screenshot for reference.

Note: Issue occurs only in Release mode.

image002

Regarding the suggested workaround, it works for us skipping the XamlCompilation.

Sample link: CustomThemeTesting 1.zip

Choza-rajan avatar Feb 16 '24 05:02 Choza-rajan

@Choza-rajan what you're describing is exactly what the PR is for. Investigating if the PR is in the nightly you tested or not...

StephaneDelcroix avatar Feb 16 '24 07:02 StephaneDelcroix

@Choza-rajan in order to debug or get a stack trace, you could force XamlCompilation on DEBUG builds by setting MauiXamlCValidateOnly to false in your csproj. By doing that, you won't get the tooling you usually get (hot reload, etc, ...

StephaneDelcroix avatar Feb 16 '24 07:02 StephaneDelcroix

the fix isn't available in 8.0.7, but should be in 8.0.10-nightly.10044

StephaneDelcroix avatar Feb 16 '24 13:02 StephaneDelcroix