locked
Binding to ApplicationData LocalSettings/RoamingSettings Values crashes app RRS feed

  • Question

  • Im trying to bind directly to the Values property of ApplicationData RoamingSettings container but the app crashes. The value I type in the textbox get saved and loaded but the App does crash instantly after the value changes thru the binding. My question is should you bind to Values directly or use LoadState/SaveState?

    I would make it really easy to bind to settings in XAML. Does the RoamingSettings implement IObservableMap correctly or is there some disposing happening? My code is based on the BlankPage template. Im currently instyalling a Windows 87 in Hyper-V to se if it is only my computer.

    <Page
        x:Class="App10.MainPage"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="using:App10"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        mc:Ignorable="d"
        DataContext="{Binding Settings, RelativeSource={RelativeSource Self}}">
    
        <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
            <TextBox Text="{Binding Hello,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" Margin="310,249,620,491"/>
        </Grid>
    </Page>

    And Codefile

    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using System.Runtime.InteropServices.WindowsRuntime;
    using Windows.Foundation;
    using Windows.Foundation.Collections;
    using Windows.Storage;
    using Windows.UI.Xaml;
    using Windows.UI.Xaml.Controls;
    using Windows.UI.Xaml.Controls.Primitives;
    using Windows.UI.Xaml.Data;
    using Windows.UI.Xaml.Input;
    using Windows.UI.Xaml.Media;
    using Windows.UI.Xaml.Navigation;
    
    // The Blank Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=234238
    
    namespace App10
    {
        /// <summary>
        /// An empty page that can be used on its own or navigated to within a Frame.
        /// </summary>
        public sealed partial class MainPage : Page
        {
            public IPropertySet Settings { get; set; }
            public MainPage()
            {
                Settings = ApplicationData.Current.LocalSettings.CreateContainer("MySettings", ApplicationDataCreateDisposition.Always).Values;
                if (!Settings.ContainsKey("Hello"))
                {
                    Settings.Add("Hello", "StartingValue");
                }
                this.InitializeComponent();
            }
        }
    }
    

    Monday, December 30, 2013 11:04 PM

Answers

  • http://stackoverflow.com/questions/9982692/attempted-to-read-or-write-protected-memory-this-is-often-an-indication-that-ot?rq=1

    Appears to be that bindings are lamdas that are not thread-safe. That why it crashes with await and typing in the textbox. It work some developers since its random if an other thread is used.

    Saturday, January 4, 2014 11:00 AM

All replies

  • Hi Fredrik,

    I helped you test your code in my environment and works fine, nothing wrong, I can see the TextBox displayed with a "StartingVale" as its text. Could you try your code in another develop machine to see if the same thing happens?

    --James


    <THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
    Thanks
    MSDN Community Support

    Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    Tuesday, December 31, 2013 6:26 AM
    Moderator
  • I have now tested the code in a VM. I get the same results. I should should also add that the App exits with the following message."The program '[4704] App10.exe: Program Trace' has exited with code 0 (0x0).
    The program '[4704] App10.exe' has exited with code -1073741819 (0xc0000005) 'Access violation'."

    I have a debugger attached (F5 with Debug build) but it pops up a window that asks if i would like to attach debugger, same on both machines. Log from Event log reveals

    Faulting application name: App1.exe, version: 1.0.0.0, time stamp: 0x52c3dab2
    Faulting module name: Windows.UI.Xaml.dll, version: 6.3.9600.16408, time stamp: 0x523d44f3
    Exception code: 0xc0000005
    Fault offset: 0x00870124
    Faulting process ID: 0xaf8
    Faulting application start time: 0x01cf06d0dc642e17
    Faulting application path: c:\users\fredrik\documents\visual studio 2013\Projects\App1\App1\bin\Debug\AppX\App1.exe
    Faulting module path: C:\Windows\System32\Windows.UI.Xaml.dll
    Report ID: 22adad81-72c4-11e3-8251-00155d280301
    Faulting package full name: fc2d9c68-5cf4-413d-8af5-34b70c530731_1.0.0.0_neutral__c2jbtyard3qg2
    Faulting package-relative application ID: App

    Wednesday, January 1, 2014 9:14 AM
  • same issue here . . .

    The program '[8580] App3.exe' has exited with code -1073741819 (0xc0000005) 'Access violation'.
    • Edited by Klaus Schulz Wednesday, January 1, 2014 10:15 AM
    Wednesday, January 1, 2014 10:15 AM
  • Wednesday, January 1, 2014 11:15 AM
  • I have also now tested it on a tablet with Windows 8.1 Pro 32-bit. Same error
    Wednesday, January 1, 2014 11:20 AM
  • Im thinking maybe there is some marshalling between threads that is not taken care of correctly. The PropertySet can have values that are objects and not primitives.

    Ive added

     public IPropertySet Settings { get; set; }
            public MainPage()
            {
                Settings = ApplicationData.Current.LocalSettings.CreateContainer("MySettings", ApplicationDataCreateDisposition.Always).Values;
                if (!Settings.ContainsKey("Hello"))
                {
                    Settings.Add("Hello", "StartingValue");
                }
                this.InitializeComponent();
                ChangeValue();
            }
            public async Task ChangeValue()
            {
                await Task.Delay(10000);
                Settings["Hello"] = DateTime.Now.ToString(); //This crashes
            }

    The value get saved but now i get the Exception out!!

    System.AccessViolationException was unhandled
      HResult=-2147467261
      Message=Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
      Source=mscorlib
      StackTrace:
           at System.Runtime.InteropServices.WindowsRuntime.IMap`2.Insert(K key, V value)
           at System.Runtime.InteropServices.WindowsRuntime.MapToDictionaryAdapter.Insert[K,V](IMap`2 _this, K key, V value)
           at System.Runtime.InteropServices.WindowsRuntime.MapToDictionaryAdapter.Indexer_Set[K,V](K key, V value)
           at App10.MainPage.<ChangeValue>d__0.MoveNext()
           at System.Runtime.CompilerServices.AsyncMethodBuilderCore.MoveNextRunner.InvokeMoveNext(Object stateMachine)
           at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
           at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
           at System.Runtime.CompilerServices.AsyncMethodBuilderCore.MoveNextRunner.Run()
           at System.Runtime.CompilerServices.AsyncMethodBuilderCore.<>c__DisplayClass2`1.<OutputAsyncCausalityEvents>b__0()
           at System.Runtime.CompilerServices.AsyncMethodBuilderCore.ContinuationWrapper.Invoke()
           at System.Runtime.CompilerServices.TaskAwaiter.<>c__DisplayClass1.<OutputWaitEtwEvents>b__0()
           at System.Runtime.CompilerServices.AsyncMethodBuilderCore.ContinuationWrapper.Invoke()
           at System.Threading.Tasks.SynchronizationContextAwaitTaskContinuation.<>c__DisplayClass4.<GetActionLogDelegate>b__3()
           at System.Threading.Tasks.SynchronizationContextAwaitTaskContinuation.<.cctor>b__6(Object state)
           at System.Threading.WinRTSynchronizationContext.Invoker.InvokeCore()
           at System.Threading.WinRTSynchronizationContext.Invoker.InvokeInContext(Object thisObj)
           at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
           at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
           at System.Threading.WinRTSynchronizationContext.Invoker.Invoke()
      InnerException:

    Wednesday, January 1, 2014 11:31 AM
  • More testing reveals that when setting binding Mode to OneTime makes

    await Task.Delay(2000);
    Settings["Hello"] = DateTime.Now.ToString();

    the app not crash. Think makes me think there is a problem in Windows.Xaml.UI.dll it does not handle events from the wrong thread.

    Wednesday, January 1, 2014 2:20 PM
  • Hi Fredrik,

    Thank you for sharing the code with us, but sorry I still cannot reproduce it on my desktop.

    I will consult some senior engineers to see if they can provide more help.

    --James


    <THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
    Thanks
    MSDN Community Support

    Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    Thursday, January 2, 2014 8:42 AM
    Moderator
  • That would be great! Any more information you need from me?
    Thursday, January 2, 2014 12:44 PM
  • http://stackoverflow.com/questions/9982692/attempted-to-read-or-write-protected-memory-this-is-often-an-indication-that-ot?rq=1

    Appears to be that bindings are lamdas that are not thread-safe. That why it crashes with await and typing in the textbox. It work some developers since its random if an other thread is used.

    Saturday, January 4, 2014 11:00 AM