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

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

Open
jhm-ciberman opened this issue Dec 16, 2023 · 0 comments

Comments

@jhm-ciberman
Copy link

Hi! First, thanks for writting this amazing library. I love it. 💖

Secondly, I am using this library in my game developed using a custom game engine.
The bindings work fine but I found this special case when it crashes with a fatal AccessViolationException.

The code in question is something like this:

public class BuildModeFurniturePreviewNode : ObservableObject, IBuildModeTab, IDisposable
{
    private readonly BuildModeActivityViewModel _parent;
    private readonly EBinding _bindings;
    public BuildModeFurniturePreviewNode(BuildModeActivityViewModel parent)
    {
        this._parent = vm;

        // More stuff here...

        this._bindings = new EBinding
        {
            () => this.CursorWorldPosition == this._parent.CursorWorldPosition,
        };
    }

    public void Dispose()
    {
        this._bindings.Dispose();
    }

    // More stuff here...
}


public class BuildModeActivityViewModel : ObservableObject
{
    private readonly List<IBuildModeTab> _tabs = new();

    public IReadOnlyList<IBuildModeTab> Tabs => this._tabs;

    public BuildModeActivityViewModel()
    {
        this.ChangeTabCommand = new RelayCommand<IBuildModeTab>(tab => this.CurrentTab = tab!, (tab) => this.CurrentTab != tab);

        this._tabs.AddRange(new IBuildModeTab[]
        {
            new FurnitureTabViewModel(this),

            // More tabs here...
            //new FloorsTabViewModel(this),
            //new WallsTabViewModel(this),
            //new RoofsTabViewModel(this),
        });

        this._currentTab = this._tabs[0];
    }

    private IBuildModeTab _currentTab;
    public IBuildModeTab CurrentTab
    {
        get => this._currentTab;
        set => this.SetProperty(ref this._currentTab, value);
    }

    // More stuff here...
}

I this the problem is when I try to bind to properties of a parent object that hasn't finished to run it's constructor code.

Here is the stack trace:

Fatal error. System.AccessViolationException: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
   at System.Runtime.CompilerServices.CastHelpers.ChkCastInterface(Void*, System.Object)
   at EBind.TriggerDelegate+<>c.<.ctor>b__1_1(System.Object, System.Object)
   at EBind.ExpressionBinding.Subscribe(System.Collections.Generic.IEnumerable`1<EBind.Trigger>, System.Action)
   at EBind.EqualityBindings.EqualityBinding`1[[System.Nullable`1[[System.Numerics.Vector2, System.Private.CoreLib, Version=7.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]], System.Private.CoreLib, Version=7.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]]..ctor(System.Linq.Expressions.Expression, System.Linq.Expressions.Expression, EBind.BindFlag, EBind.Configuration)
   at EBind.EqualityBindings.EqualityBindingFactory`1[[System.Nullable`1[[System.Numerics.Vector2, System.Private.CoreLib, Version=7.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]], System.Private.CoreLib, Version=7.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]].Create(System.Linq.Expressions.Expression, System.Linq.Expressions.Expression, EBind.BindFlag, EBind.Configuration)
   at EBind.EqualityBindings.EqualityBinding.Create[[System.Boolean, System.Private.CoreLib, Version=7.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]](System.Linq.Expressions.Expression`1<System.Func`1<Boolean>>, EBind.BindFlag, EBind.Configuration)
   at EBind.ExpressionBinding.Create[[System.Boolean, System.Private.CoreLib, Version=7.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]](System.Linq.Expressions.Expression`1<System.Func`1<Boolean>>, EBind.BindFlag, EBind.Configuration)
   at EBind.EBinding.Add[[System.Boolean, System.Private.CoreLib, Version=7.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]](System.Linq.Expressions.Expression`1<System.Func`1<Boolean>>, Int32)
   at LifeSim.ViewModels.BuildMode.FurnitureTabViewModel..ctor(LifeSim.ViewModels.BuildMode.BuildModeActivityViewModel)
   at LifeSim.ViewModels.BuildMode.BuildModeActivityViewModel..ctor(LifeSim.ViewModels.GameViewModel)
   at LifeSim.ViewModels.GameViewModel.SwitchGameMode()
   at LifeSim.ViewModels.GameViewModel.Input_ActionPerformed(System.Object, LifeSim.ViewModels.ActionPerformedEventArgs)
   at LifeSim.Game.GameInputProvider.OnActionPerformed(LifeSim.ViewModels.ActionType)
   at LifeSim.Game.GameInputProvider.Input_KeyPressed(System.Object, Veldrid.KeyEvent)
   at LifeSim.Imago.Input.InputManager.UpdateFrameInput()
   at LifeSim.Imago.Application.Run()
   at LifeSim.Game.Program.Main(System.String[])

Of course I could use the classic PropertyChanged event here and subscribe manually to that change. It's not a big deal, just wanted to report the bug because it's a bit annoying.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant