Programs randomly getting System.AccessViolationException
Asked Answered
S

6

13

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

Sushi answered 18/6, 2014 at 20:32 Comment(14)
You get that exception in System.Windows.Forms.dll by writing a console application. Are you sure that we can reproduce that exception by just running your code?Sirkin
Exactly my point. Its NOT my code. But something to do with Visual Studio I guess. If I have to, I can make a video lol to prove that code produces the error.Sushi
probably debugger probing/reflecting variables...Parliamentary
@JABFreeware, Perhaps its a Visual studio extension? The video would be good as it would help us see where the exception is generated from.Heartbroken
What you describe is highly unlikely. Is there any other project in the solution? Does this happen when you're not running under the debugger? Is your console program even linking to Windows.Forms.Dll? Do you have any Visual Studio extensions enabled? (If so, disable them.) I can't reproduce your problem. Nor, apparently, can anybody else here. So there must be something different in your configuration.Albert
@JimMischel I added oneSushi
How about answering the questions in my earlier comment? Also, post the full exception message, including the stack trace, and any other pertinent information. From what little I can glean from the video, it looks like it might be just a timing issue: Visual Studio having trouble restarting your program so quickly after you closed it. Does this happen if you wait five seconds between when you close your program and when you start it again?Albert
@JimMischel hmmmm looked fine before I uploaded it :/ Anyway...yes normally if I wait its fine. Emphasis on normally. Still does it sometimes. It doesnt happen unless debugging, and no of course it never references that dll.Sushi
@JimMischel added stack trace.Sushi
No idea. If you're not referencing the Windows Forms stuff, then it must be Visual Studio or some Visual Studio extension that you've installed. I can only say that I've never run into this problem.Albert
You've tried restarting / cleaning the project / resetting vs settings / making a new project?Lillalillard
@CCInc Everything but resetting vs settings. I like my settings :(Sushi
@JABFreeware It's a known fact that VS has some issues with settings. You can always back them up and if it doesn't help, restore them. https://mcmap.net/q/112435/-how-do-i-truly-reset-every-setting-in-visual-studio-2012Lillalillard
I had the same issue. My code autocomplete from database on textchanged event. When I moved the code to form_load, I didn't get the error anymore.Mariejeanne
L
9

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.

Limon answered 26/6, 2015 at 13:6 Comment(0)
C
6

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

Caboose answered 5/8, 2016 at 16:7 Comment(2)
Link is dead :(Richel
Worked like a charm on VS2017.Kephart
S
4

http://veriworld.verivox.ads/docs/DOC-6011?et=watches.email.document_comment#comment-4629

Seems that in some cases VS/.net framework has memory trampling issues. I really wish people would stop saying 'it cant be' when someone says something breaks.

Succinct answered 5/1, 2015 at 16:7 Comment(1)
The link is not available any more :(Button
B
1

There is nothing that looks like it may cause issues.

Did you try enabling breaking on exception? This will cause your code to pause when it encounter an exception.

enter image description here

Bridgid answered 18/6, 2014 at 20:49 Comment(2)
This seems to be a problem with the IDE. Code runs fine if I don't step through it, but I get access violations when I do.Cantrip
I don't have the solution to the problem but this almost certainly isn't an IDE problem. This behaviour is usually indicative of a threading issue, whereby you're getting some kind of race condition which goes one way on the step through and the other way when you run it straight up.Lymphocytosis
S
1

I can confirm this problem. It occurs in a small (very trivial) WinForms program distributed with my own commercial app. It's never happened in any earlier version of VS going back to VS2005, including those run on the same machine. It only occurs in VS2013. My version of VS has no 3rd-party add-ins other than what's installed with VS itself. Problem occurs even when all code in "Main()" is commented out. I've tried rebooting, disabling virus checker, firewall, etc. Makes no difference which version of .NET my app targets either (2.0, 3.5, 4.0, whatever). It happens frequently but randomly. Sometimes it works, sometimes it doesn't. Go figure.

Strongarm answered 13/1, 2015 at 18:43 Comment(3)
I just discovered connect.microsoft.com/VisualStudio/feedback/details/819552/… which discusses numerous scenarios that may impact the situation. Installing .NET 4.5.2 didn't solve it for me, but changing the "Platform target" from "Any CPU" to "x86" did (on the "Build" property page). Not the ideal situation, but it's a start.Strongarm
I get this when debugging a fairly complex Windows Forms application in VS 2015 Enterprise. The error is being thrown by the IBM iDbAccess library, and happens consistently when debugging. If I do not step through, my error logging still picks it up, so the debugging is causing the program to break on the exception, but the exception is happening behind the scenes anyway. I haven't checked to see if the Release configuration has this problem, and it never seems to impact the compiled executable.Comyns
I am experiencing a problem Similar to what Joseph Morgan is describing. I only get the error if I step through disassembled code from an external .NET library. I think thatiDbAccess may be the origin of my problem although the error is thrown by ...UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop. Is anybody else experiencing iDbAccess problems associated with this message?Fredella
A
1

In my case, such VS behavior was caused by Comodo Firewall.

The solution is adding VS Projects folder to Comodo HIPS exclusions list (even if the HIPS is turned off!).

Here is a related thread Comodo forum: https://forums.comodo.com/empty-t98149.0.html

Ackler answered 8/9, 2016 at 3:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.