0

Having trouble with 'System.AccessViolationException' while developing a WindowsPhone8 app and don't know how to deal with it. Here is GitHub repo.

Exception appears when app tries to access (assign value 5) int field of an object ("value") of a class ("SudokuCell") in a class ("SudokuSection") in a class ("SudokuField").

testSudokuField.AddValueToSingleCell(5, new Tuple<int, int>(i, j)); // class MainPage

public class SudokuField
{
    public SudokuSection[,] Sections;
    public SudokuHorizontal[] Horizontals;
    public SudokuVertical[] Verticals; 
    public bool solved;

public void AddValueToSingleCell(int value, Tuple<int, int> coords) // Item1 - horizontal, Item2 - vertical
    {
        var temp = InterpretCoordsForSection(coords);

        this.Sections[temp.Item1 - 1, temp.Item2 - 1].ChangeCellValue(value, temp.Item3, temp.Item4);
        this.Horizontals[coords.Item1 - 1].cells[coords.Item2 - 1].value = value;
        this.Verticals[coords.Item2 - 1].cells[coords.Item1 - 1].value = value;
    }

public class SudokuSection
{
    public SudokuCell[,] cells;

    // --------- Constructors
    public SudokuSection()
    {
        this.cells = new SudokuCell[3, 3];
        for (int i = 0; i < 3; i++)
            for (int j = 0; j < 3; j++)
                this.cells[i, j] = new SudokuCell();
    }

    public void ChangeCellValue(int value, int x, int y)
    {
        this.cells[x-1, y-1].ChangeValue(value);
    }
}

public class SudokuCell
{
    public int value; // Exect value of a cell
    public SortedSet<int> acceptableValues; // A of acceptable values of a single cell

    // --------- Constructors
    public SudokuCell() // Empty constructor
    {
        this.acceptableValues = new SortedSet<int>();
    }
    public SudokuCell(int value) // Constructor for a cell with an exect value
    {
        this.value = value;
    }

    // --------- Methods
    public void ChangeValue(int value)
    {
        this.value = value;
    }
}

I tried to explain the issue maximally understandable. Will be grateful if you just point me to a solution.

Exception http://cs609829.vk.me/u33859447/docs/890ffbe19d74/question1.png?extra=UlBQBDfFTVi4oKt83aRDNfDrQcjh6AeNYv0aUQ1MmwDHWILYUR3EXmP82OylatDnm0BiT5wZBKYvqEcmQfLqLZX6bpJ7aCo

Corio
  • 395
  • 7
  • 20
  • What does `addValueToSingleCell` do? As a sidenote: when looking at an error like this, you should focus on the deepest level that it occurs at, everything else is just the stacktrace showing you what exactly lead up to that moment. – Jeroen Vannevel Aug 03 '14 at 15:55
  • We have an empty Sudoku field at start. Then we input test data: `AddValueToSingleCell(5, new Tuple(i, j))` where `5` is value of a cell and `(i, j)` - it's coordinates. I'm kind of newbie in programming and I've just started exploring WP8 platform. So deepest levels are sealed for me. – Corio Aug 03 '14 at 16:01
  • Can you edit your code to show the (relevant) content of `AddValueToSingleCell`? – Jeroen Vannevel Aug 03 '14 at 16:05
  • Ready. Actually this method just calls three another methods. So the problem, I think, is somewhere deeper. – Corio Aug 03 '14 at 16:13
  • Do you ever interact with the filesystem from these methods? – Jeroen Vannevel Aug 03 '14 at 16:16
  • Nope, I didn't. (If I understand what do you mean) – Corio Aug 03 '14 at 16:18
  • Have you already tried restarting your computer and perhaps a few services? Other questions seem to indicate that helps. http://stackoverflow.com/questions/596413/attempted-to-read-or-write-protected-memory – Jeroen Vannevel Aug 03 '14 at 16:19
  • I've just restarted computer and phone, but exception continues to appear. Don't think I can apply those answers. Considering that I deploy on WP8, and those use .net 2.0 – Corio Aug 03 '14 at 17:08

1 Answers1

1

Absolutely stupid mistake: testSudokuField = new SudokuField();

Corio
  • 395
  • 7
  • 20