Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Programs randomly getting System.AccessViolationException

Okay so I have been having a lot of issues with debugging. I'm using VS2013 Pro and Windows 8.1. Both are up to date. The issue is, when I start debugging, half the time it throws this error:

An unhandled exception of type 'System.AccessViolationException' occurred in System.Windows.Forms.dll

Additional information: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.

Its not my code's fault either. I made a simple test as an example below. Note that I am not referencing System.Windows.Forms from this app.

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;

    namespace ConsoleApplication2
    {
        class Program
        {
            static void Main(string[] args)
            {
                List<int> testing = new List<int>();
                for(int i =0; i < 50; i++)
                {
                    testing.Add(i);
                }

                for (int i = 0; i < 50; i++)
                {
                    Console.WriteLine(testing[i].ToString());

                }

                Console.ReadLine();
            }
        }
    }

I have no idea what's causing this. It will work if I click okay and run it again, most the time. Occasionally I have to do it twice.

Any ideas?

Stack trace:

enter image description here

like image 723
FrostyFire Avatar asked Jun 18 '14 20:06

FrostyFire


2 Answers

I also ran into this problem on windows10 using visual studio express2015. building to x86 did not fix my problem but the following workaround did the trick:

This issue is caused by the code which gathers return values. It is possible to work around the issue by disabling Managed return values.

  1. Go to the System properties (Win8: WinKey+X, select ‘System’, Win7: Open ‘Properties’ from my computer)
  2. Advanced System Settings
  3. Environment Variables…
  4. Click ‘New’ and add
    • Name: VSDebug_DisableManagedReturnValue
    • Value: 1

source: https://connect.microsoft.com/VisualStudio/feedback/details/819552/visual-studio-debugger-throws-accessviolationexception

like image 154
craig Avatar answered Sep 22 '22 01:09

craig


Changing the Platform Target to x86 works for me, It came to a point that stepping through the code was impossible without the forementioned Exception. I run Win 8.1 64-bit and Visual Studio 2013.

like image 42
Null Avatar answered Sep 26 '22 01:09

Null