Search code examples
c#exceptionvisual-studio-2012windows-store-appsaccess-violation

InitializeComponent - AccessViolationException


I'm working on a Windows Store Application and I've run into a problem.

I left my computer with a fully functionally program with no errors. After a couple of days away from the computer I started it and tried to run my program. It immediatly crashed and stated: "AccessViolationException was unhandled. Attempted to read or write protected memory. This is often an indication that other memory is corrupt." at the InitializeComponent.

I can however change in my App.Xaml.cs file the StartPage to the Mainpage in the OnLaunched method and it workes fine. But then I dont have a Login page. And I cant redirect from the Mainpage to the LoginPage or else it crashes.

My CallStack [External Code]

EmployeeAssistant.exe!EmployeeAssistant.PagesWithSnappedViews.LoginPage.LoginPage() Line 49 + 0x8 bytes C# [External Code] EmployeeAssistant.exe!EmployeeAssistant.App.OnLaunched(Windows.ApplicationModel.Activation.LaunchActivatedEventArgs args) Line 58 + 0x42 bytes C# [External Code]

My Locals enter image description here

-------------------Edit-------------------

    private readonly ObservableCollection<PropertyInfo> _colorSource = new ObservableCollection<PropertyInfo>();
    private string _selectedColorName;

    public ObservableCollection<PropertyInfo> ColorSource
    {
        get { return _colorSource; }
    }
    public string SelectedColorName
    {
        get { return _selectedColorName; }
        set
        {
            _selectedColorName = value;
            OnPropertyChanged();
        }
    }

    public LoginPage()
    {

            InitializeComponent();

            var colors = typeof(Colors).GetTypeInfo().DeclaredProperties;

            foreach (var item in colors)
            {
                ColorSource.Add(item);
            }

    }

The App.OnLaunched:

    /// <summary>
    /// Invoked when the application is launched normally by the end user.  Other entry points
    /// will be used when the application is launched to open a specific file, to display
    /// search results, and so forth.
    /// </summary>
    /// <param name="args">Details about the launch request and process.</param>
    protected override void OnLaunched(LaunchActivatedEventArgs args)
    {
        Frame rootFrame = Window.Current.Content as Frame;

        // Do not repeat app initialization when the Window already has content,
        // just ensure that the window is active
        if (rootFrame == null)
        {
            // Create a Frame to act as the navigation context and navigate to the first page
            rootFrame = new Frame();

            if (args.PreviousExecutionState == ApplicationExecutionState.Terminated)
            {
                //TODO: Load state from previously suspended application
            }

            // Place the frame in the current Window
            Window.Current.Content = rootFrame;
        }

        if (rootFrame.Content == null)
        {
            // When the navigation stack isn't restored navigate to the first page,
            // configuring the new page by passing required information as a navigation
            // parameter
            if (!rootFrame.Navigate(typeof(LoginPage), args.Arguments))
            {
                throw new Exception("Failed to create initial page");
            }
        }
        // Ensure the current window is active
        Window.Current.Activate();
    }

If i try to debug to see what is happening all it does is to load code from the LayoutAwarePage and this row: private readonly ObservableCollection _colorSource = new ObservableCollection(); Then it goes to InitializeComponent and I get AccessViolationException.

Does anyone have an idea of what is happening and maybe how to solve this?

Niklas


Solution

  • What I did was to create a basic page and redid my page. And now it works just fine. Dont know what was the problem though. But thats the way I solved it.