Xamarin.forms: System.AccessViolationException' occurred in Unknown Module

Created on 7 Feb 2018  ·  16Comments  ·  Source: xamarin/Xamarin.Forms

vsissue2
Bug report best practices: https://github.com/xamarin/Xamarin.Forms/wiki/Submitting-Issues

Description

An unhandled exception of type 'System.AccessViolationException' occurred in Unknown Module.
Attempted to read or write protected memory. This is often an indication that other memory is corrupt.

Steps to Reproduce

//working --MainPage.cs
async void Page1(object sender, EventArgs e)
{
await Navigation.PushAsync(new xmlscreen2());
}
Not working-- Page1.cs
void GoForTable()
{
await Navigation.PushAsync(new xmlscreen1());
}
Both page1 and page2 having simple xaml code.
It is not working only for Widows project in both debug and Release mode. Solution plateform is x86.
using VS2017 with 15.5.5 update and latest Xamarin sable version.

Expected Behavior

Actual Behavior

Basic Information

  • Version with issue:
  • Last known good version:
  • IDE: VS 2017
  • Platform Target Frameworks:

    • UWP:

  • Nuget Packages: 4.5.0
  • Affected Devices: Local machine

Screenshots

vsissue1

Reproduction Link

6 help wanted high inactive UWP needs-info ❓ bug up-for-grabs

All 16 comments

I cant reproduce this. Can you please provide a reproduction case?

@arvindrajachourasiya Have you been able to create a reproduction project? Thanks

Closing this for now (no response). Feel free to reopen it if new information comes to light.

@vijaypalve please provide sample for XF team

@samhouts please reopen thes issue

here is the link with reproduction https://github.com/AndreiMisiukevich/CardView/issues/213#issuecomment-501689893

@AndreiMisiukevich Aside from the error message, this doesn't look to be the same at all. I am wondering if the BatchCommit that you're doing in the IndicatorsControl is conflicting with commits we're doing. Needs more investigation. Thanks!

@samhouts sure, i will try to create pre-release version without batching for UWP.

@jfversluis @samhouts

Can reproduce this issue even without Batch updates in IndicatorsControl

https://github.com/AndreiMisiukevich/CardView/issues/213#issuecomment-502522765

So you can find reproduction if go through provided link)

I run the reproduction.

Here the stacktrace of the crash.

Xamarin.Forms.Platform.UAP.dll!Xamarin.Forms.Platform.UWP.FrameRenderer.UpdateBackgroundColor()
    Xamarin.Forms.Platform.UAP.dll!Xamarin.Forms.Platform.UWP.VisualElementRenderer<Xamarin.Forms.Frame, Windows.UI.Xaml.Controls.Border>.OnElementPropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
    Xamarin.Forms.Platform.UAP.dll!Xamarin.Forms.Platform.UWP.FrameRenderer.OnElementPropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
    Xamarin.Forms.Core.dll!Xamarin.Forms.BindableObject.OnPropertyChanged(string propertyName)
    Xamarin.Forms.Core.dll!Xamarin.Forms.Element.OnPropertyChanged(string propertyName)
    Xamarin.Forms.Core.dll!Xamarin.Forms.BindableObject.ClearValue(Xamarin.Forms.BindableProperty property, bool fromStyle, bool checkAccess)
    Xamarin.Forms.Core.dll!Xamarin.Forms.Setter.UnApply(Xamarin.Forms.BindableObject target, bool fromStyle)
    Xamarin.Forms.Core.dll!Xamarin.Forms.Style.UnApplyCore(Xamarin.Forms.BindableObject bindable, Xamarin.Forms.Style basedOn)
    Xamarin.Forms.Core.dll!Xamarin.Forms.Style.Xamarin.Forms.IStyle.UnApply(Xamarin.Forms.BindableObject bindable)
    Xamarin.Forms.Core.dll!Xamarin.Forms.MergedStyle.SetStyle(Xamarin.Forms.IStyle implicitStyle, System.Collections.Generic.IList<Xamarin.Forms.Style> classStyles, Xamarin.Forms.IStyle style)
    Xamarin.Forms.Core.dll!Xamarin.Forms.MergedStyle.Style.set(Xamarin.Forms.IStyle value)
    Xamarin.Forms.Core.dll!Xamarin.Forms.NavigableElement..cctor.AnonymousMethod__20_0(Xamarin.Forms.BindableObject bindable, object oldvalue, object newvalue)
    Xamarin.Forms.Core.dll!Xamarin.Forms.BindableObject.SetValueActual(Xamarin.Forms.BindableProperty property, Xamarin.Forms.BindableObject.BindablePropertyContext context, object value, bool currentlyApplying, Xamarin.Forms.Internals.SetValueFlags attributes, bool silent)
    Xamarin.Forms.Core.dll!Xamarin.Forms.BindableObject.SetValueCore(Xamarin.Forms.BindableProperty property, object value, Xamarin.Forms.Internals.SetValueFlags attributes, Xamarin.Forms.BindableObject.SetValuePrivateFlags privateAttributes)
    Xamarin.Forms.Core.dll!Xamarin.Forms.BindableObject.SetValue(Xamarin.Forms.BindableProperty property, object value, bool fromStyle, bool checkAccess)
    PanCardView.dll!PanCardView.Controls.IndicatorsControl.ApplySelectedStyle(Xamarin.Forms.View view, int index)
    PanCardView.dll!PanCardView.Controls.IndicatorsControl.ApplyStyle(Xamarin.Forms.View view, int cyclingIndex)
    PanCardView.dll!PanCardView.Controls.IndicatorsControl.OnResetIndicatorsStyles(int currentIndex)
    PanCardView.dll!PanCardView.Controls.IndicatorsControl.ResetIndicatorsStylesNonBatch()
    PanCardView.dll!PanCardView.Controls.IndicatorsControl.ResetIndicatorsStyles()
    PanCardView.dll!PanCardView.Controls.IndicatorsControl..cctor.AnonymousMethod__13_0(Xamarin.Forms.BindableObject bindable, object oldValue, object newValue)

The crash is triggered by the background set of the native control.

On UWP this kind of crash is often related to threads dispatching problem.

I tested to add a BeginInvokeOnMainThread and I confirm it fix the issue for me.

So you have two ways to fix this :

  • Use a custom renderer like below (user side)
  • Wrap your event handler with BeginInvokeOnMainThread (library side)
[assembly: ExportRenderer(typeof(Frame), typeof(CustomFrameRenderer))]
namespace App1.UWP.Renderers
{
    public class CustomFrameRenderer : FrameRenderer
    {
        protected override void UpdateBackgroundColor()
        {
            Device.BeginInvokeOnMainThread(() => base.UpdateBackgroundColor());
        }
    }
}

So for me, the issue is not in Xamarin.Forms.

@kvpt thanks Kevin)
But i cannot agree with you in this case.
Changing color mustn't crash the app even if this action is called from background thread.
I think, XF should manage / redirect such actions to main thread.

am i wrong?

if Thread.CurrentThread.IsBackground then wrap action ```Dispatcher.BeginInvoke(() => { }) else leave as is

@AndreiMisiukevich I think I remember that the Xamarin Teams already have an issue like this in the past and decided that it was the way the platform worked and decided to leave it to the developper. But i'm not sure now.
Wait and see what the team decides to do, because it's a big change to do if they have to ensure that all control access are done on the ui tread.

@samhouts
need your sight )

Our entire approach to threading is going to be revisited as we move toward a multi-window solution. We're currently investigating similar problems (#6189), and we're not yet prepared to add more thread management until we've figured out the consequences. That said, we may find a way to prevent this type of exception during that investigation, although it won't be in the short term. I'll mark this as ready for work just so we don't lose sight of it. Thanks!

This issue doesn't seem to have had any activity in a long time. We're working on prioritizing issues and resolving them as quickly as we can. To help us get through the list, we would appreciate an update from you to let us know if this is still affecting you on the latest version of Xamarin.Forms, since it's possible that we may have resolved this as part of another related or duplicate issue. If we don't see any new activity on this issue in the next 30 days, we'll evaluate whether this issue should be closed. Thank you!

Since we haven't heard from you in more than 30 days, we hope this issue is no longer affecting you. If it is, please reopen this issue and provide the requested information so that we can look into it further. Thank you!

Was this page helpful?
0 / 5 - 0 ratings