Search code examples
vb.netvisual-studio-2010access-violation

AccessViolationException when typing in Textbox


About a week ago a program I'm working on began crashing with an AccessViolationException whenever I typed into a certain textbox. I've reduced it to this test case, but I'm out of ideas:

Imports System.Windows.Forms

Public Class Start
    Inherits System.Windows.Forms.Form

    <STAThread()>
    Public Shared Sub Main(args() As String)
        Application.EnableVisualStyles()
        Application.Run(New Start())
    End Sub

    Private txtNotes As System.Windows.Forms.TextBox
    Public Sub New()
        Me.txtNotes = New System.Windows.Forms.TextBox()
        Me.txtNotes.Multiline = True
        Me.Controls.Add(Me.txtNotes)
    End Sub
End Class

When I build this program using Visual Studio, it crashes if I type into the textbox (always). If I build it using vbc.exe Start.vb /target:winexe /main:Start /out:Start.exe, it works fine.

Additionally, I've found that removing either of these lines stops it from crashing using Visual Studio:

Application.EnableVisualStyles()
Me.txtNotes.Multiline = True

I've already done a repair installation of Visual Studio, and used sfc.exe to check for corrupted system files. I've tried searching for existing solutions to this problem, but haven't found anything. What could be causing Visual Studio to build a broken executable? Is there some setting that would do this?

Edit: I'm also running AVG antivirus at the moment. I recall that there was an AVG update around the time these crashes began. As per one of the comments below, disabling AVG, and then scanning the executable prevents it from crashing. This would seem to indicate that the problem lies with AVG.


Solution

  • We have the same problem with a few of our clients' installations on Windows 7. Our product is C# with WinFoms and .net 4.0. Typing in a mutli-line text box under WinForms crashes an AccessViolationException. You can paste text in the box and delete it too, just not type. Single line text boxes are fine. We also found that removing Application.EnableVisualStyles() cured the problem.

    Another way to fix it without code changes is to right click the program icon and change the compatibility settings to run the program for Windows 7.