John.Doe

I really do not know what to do about this. Often, when I run Forms programs with Visual Studio the application crashes with an exception like this "AccessViolationException". I never exerienced any problems with other standard applications like e.g. Word, Excel, Outlook, only self written programs in C# seem to suffer from this. Most of these applications then contain either something like a ToolStrip or a DataGrid which then after an exception like this are only represented by a white rectangle with a red X. The controls itself are standard out of the box.
I ran the Microsoft Memory Diagnostic today, but it did not find anything wrong with my memory. Does anybody has an idea what may cause this exception

See the end of this message for details on invoking
just-in-time (JIT) debugging instead of this dialog box.

************** Exception Text **************
System.AccessViolationException: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
at System.Drawing.SafeNativeMethods.Gdip.GdipDrawLineI(HandleRef graphics, HandleRef pen, Int32 x1, Int32 y1, Int32 x2, Int32 y2)
at System.Drawing.Graphics.DrawLine(Pen pen, Int32 x1, Int32 y1, Int32 x2, Int32 y2)
at System.Windows.Forms.DataGridViewCell.PaintBorder(Graphics graphics, Rectangle clipBounds, Rectangle bounds, DataGridViewCellStyle cellStyle, DataGridViewAdvancedBorderStyle advancedBorderStyle)
at System.Windows.Forms.DataGridViewColumnHeaderCell.PaintPrivate(Graphics g, Rectangle clipBounds, Rectangle cellBounds, Int32 rowIndex, DataGridViewElementStates dataGridViewElementState, Object formattedValue, DataGridViewCellStyle cellStyle, DataGridViewAdvancedBorderStyle advancedBorderStyle, DataGridViewPaintParts paintParts, Boolean paint)
at System.Windows.Forms.DataGridViewColumnHeaderCell.Paint(Graphics graphics, Rectangle clipBounds, Rectangle cellBounds, Int32 rowIndex, DataGridViewElementStates dataGridViewElementState, Object value, Object formattedValue, String errorText, DataGridViewCellStyle cellStyle, DataGridViewAdvancedBorderStyle advancedBorderStyle, DataGridViewPaintParts paintParts)
at System.Windows.Forms.DataGridViewCell.PaintWork(Graphics graphics, Rectangle clipBounds, Rectangle cellBounds, Int32 rowIndex, DataGridViewElementStates cellState, DataGridViewCellStyle cellStyle, DataGridViewAdvancedBorderStyle advancedBorderStyle, DataGridViewPaintParts paintParts)
at System.Windows.Forms.DataGridView.PaintColumnHeaders(Graphics g, Rectangle clipBounds, Boolean singleBorderAdded)
at System.Windows.Forms.DataGridView.PaintGrid(Graphics g, Rectangle gridBounds, Rectangle clipRect, Boolean singleVerticalBorderAdded, Boolean singleHorizontalBorderAdded)
at System.Windows.Forms.DataGridView.OnPaint(PaintEventArgs e)
at System.Windows.Forms.Control.PaintWithErrorHandling(PaintEventArgs e, Int16 layer, Boolean disposeEventArgs)
at System.Windows.Forms.Control.WmPaint(Message& m)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.DataGridView.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)


************** Loaded Assemblies **************
mscorlib
Assembly Version: 2.0.0.0
Win32 Version: 2.0.50727.42 (RTM.050727-4200)
CodeBase: file:///C:/WINDOWS/Microsoft.NET/Framework/v2.0.50727/mscorlib.dll
----------------------------------------
WindowsApplication53
Assembly Version: 1.0.0.0
Win32 Version: 1.0.0.0
CodeBase: file:///C:/Documents%20and%20Settings/XY/My%20Documents/Visual%20Studio%202005/Projects/WindowsApplication53/WindowsApplication53/bin/Debug/WindowsApplication53.exe
----------------------------------------
System.Windows.Forms
Assembly Version: 2.0.0.0
Win32 Version: 2.0.50727.42 (RTM.050727-4200)
CodeBase: file:///C:/WINDOWS/assembly/GAC_MSIL/System.Windows.Forms/2.0.0.0__b77a5c561934e089/System.Windows.Forms.dll
----------------------------------------
System
Assembly Version: 2.0.0.0
Win32 Version: 2.0.50727.42 (RTM.050727-4200)
CodeBase: file:///C:/WINDOWS/assembly/GAC_MSIL/System/2.0.0.0__b77a5c561934e089/System.dll
----------------------------------------
System.Drawing
Assembly Version: 2.0.0.0
Win32 Version: 2.0.50727.42 (RTM.050727-4200)
CodeBase: file:///C:/WINDOWS/assembly/GAC_MSIL/System.Drawing/2.0.0.0__b03f5f7f11d50a3a/System.Drawing.dll
----------------------------------------
System.Configuration
Assembly Version: 2.0.0.0
Win32 Version: 2.0.50727.42 (RTM.050727-4200)
CodeBase: file:///C:/WINDOWS/assembly/GAC_MSIL/System.Configuration/2.0.0.0__b03f5f7f11d50a3a/System.Configuration.dll
----------------------------------------
System.Xml
Assembly Version: 2.0.0.0
Win32 Version: 2.0.50727.42 (RTM.050727-4200)
CodeBase: file:///C:/WINDOWS/assembly/GAC_MSIL/System.Xml/2.0.0.0__b77a5c561934e089/System.Xml.dll
----------------------------------------
System.Web.Services
Assembly Version: 2.0.0.0
Win32 Version: 2.0.50727.42 (RTM.050727-4200)
CodeBase: file:///C:/WINDOWS/assembly/GAC_MSIL/System.Web.Services/2.0.0.0__b03f5f7f11d50a3a/System.Web.Services.dll
----------------------------------------
pnhzqoqb
Assembly Version: 1.0.0.0
Win32 Version: 2.0.50727.42 (RTM.050727-4200)
CodeBase: file:///C:/WINDOWS/assembly/GAC_MSIL/System/2.0.0.0__b77a5c561934e089/System.dll
----------------------------------------
System.Data
Assembly Version: 2.0.0.0
Win32 Version: 2.0.50727.42 (RTM.050727-4200)
CodeBase: file:///C:/WINDOWS/assembly/GAC_32/System.Data/2.0.0.0__b77a5c561934e089/System.Data.dll
----------------------------------------

************** JIT Debugging **************
To enable just-in-time (JIT) debugging, the .config file for this
application or computer (machine.config) must have the
jitDebugging value set in the system.windows.forms section.
The application must also be compiled with debugging
enabled.

For example:

<configuration>
<system.windows.forms jitDebugging="true" />
</configuration>

When JIT debugging is enabled, any unhandled exception
will be sent to the JIT debugger registered on the computer
rather than be handled by this dialog box.




Re: Windows Forms General pretty often: System.AccessViolationException

Peter Huang - MSFT

Hi John,

I think you there were some corruption with your environment.
Now I think you may try to reinstall .NET Framework to see if that works.

If you still have any concern, please feel free to post here.

Best regards,
Peter Huang






Re: Windows Forms General pretty often: System.AccessViolationException

Julesy

Hi,

We are experiencing the same problem with a .net 2.0 winforms application intermittently. It is not due to a misinstalltion as far as I can see. Any ideas

Regards

Julian

Here is the code:

Private Sub pnlTopFakeJoin_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles pnlTopFakeJoin.Paint
Dim loOutlinePen As System.Drawing.Pen
Dim loHighlightPen As System.Drawing.Pen

Try
loOutlinePen = New System.Drawing.Pen(tabWorkplaceTabs.SelectedTabOutlineColor)
loHighlightPen = New System.Drawing.Pen(tabWorkplaceTabs.SelectedTabHighlightColor)

e.Graphics.DrawLine(loOutlinePen, 0, 2, Me.Width, 2)
e.Graphics.DrawLine(loHighlightPen, 0, 3, Me.Width, 3)
Finally
If Not loOutlinePen Is Nothing Then loOutlinePen.Dispose()
If Not loHighlightPen Is Nothing Then loHighlightPen.Dispose()
End Try

End Sub

Here is the Stacktrace:

An application error has happened. Attached is an error report containing information that may be useful in working out what caused the problem.

The error message was:

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

The stack trace was System.AccessViolationException: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.

at System.Drawing.SafeNativeMethods.Gdip.GdipDrawLineI(HandleRef

graphics, HandleRef pen, Int32 x1, Int32 y1, Int32 x2, Int32 y2)

at System.Drawing.Graphics.DrawLine(Pen pen, Int32 x1, Int32 y1,

Int32 x2, Int32 y2)

at T1.Tb.Utl.Windows.Forms.TBUTL010.pnlTopFakeJoin_Paint(Object

sender, PaintEventArgs e) in

C:\build-system\build\tb_DEV6.5\tb\dev\vb.net\T1.Tb.Utl.Windows.Forms\TB

UTL010.vb:line 9047

at System.Windows.Forms.Control.OnPaint(PaintEventArgs e)

at System.Windows.Forms.Control.PaintWithErrorHandling(PaintEventArgs

e, Int16 layer, Boolean disposeEventArgs)

at System.Windows.Forms.Control.WmPaint(Message& m)

at System.Windows.Forms.Control.WndProc(Message& m)

at System.Windows.Forms.ScrollableControl.WndProc(Message& m)

at

System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)

at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message&

m)

at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)





Re: Windows Forms General pretty often: System.AccessViolationException

John.Doe

As this comes up again... There seem to be other programs which are very "sensitive" to that problem, too. The SQL Server 2005 Management Studio for example chrashes very often if you keep a query result (the grid) open and move other windows over it to have it redraw itself or minimize and maximize it. There is no real safe way for me to reproduce this error, but there seem to be some controls like the GridView (assuming the Management Studio uses it, too) that are the reason for the instability.



Re: Windows Forms General pretty often: System.AccessViolationException

PeacError

Those are the worst problems and I feel for ya...

I have had some similar issues with VS 2005. They seemed to occur if I manually tried to dispose a Dialog form. This may not help you at all, but check to see if you are manually disposing the form that contains the control where the crash is occuring. For me, the problem seemed to be that something like an event or user event would fire after the form was disposed. (even when it shouldn't have)





Re: Windows Forms General pretty often: System.AccessViolationException

Jason Gould

I was hitting this very consistently. I'm not doing any unsafe code. I'm not doing any "disposing" of anything. Basically everything was auto-generated using the form designer.

My crash happened quite regularly if my app is running, & I open then close another application, where the 2nd application would cause a redraw of my app. (So, it's apparently got something to do with redraw).

Here's the top of the stack trace where the crash was happening:

at System.Drawing.SafeNativeMethods.Gdip.GdipDrawLineI(HandleRef graphics, HandleRef pen, Int32 x1, Int32 y1, Int32 x2, Int32 y2)

at System.Drawing.Graphics.DrawLine(Pen pen, Int32 x1, Int32 y1, Int32 x2, Int32 y2)

at System.Windows.Forms.DataGridViewCell.PaintBorder(Graphics graphics, Rectangle clipBounds, Rectangle bounds, DataGridViewCellStyle cellStyle, DataGridViewAdvancedBorderStyle advancedBorderStyle)

at System.Windows.Forms.DataGridViewTextBoxCell.PaintPrivate(Graphics graphics, Rectangle clipBounds, Rectangle cellBounds, Int32 rowIndex, DataGridViewElementStates cellState, Object formattedValue, String errorText, DataGridViewCellStyle cellStyle, DataGridViewAdvancedBorderStyle advancedBorderStyle, DataGridViewPaintParts paintParts, Boolean computeContentBounds, Boolean computeErrorIconBounds, Boolean paint)

...

I beleive I have a fix for this. Double-buffer your DataGridView.

Now, annoyingly, the DoubleBuffered attribute is protected, so you have to derive from DataGridView & set it yourself. Something like this should work:

class DBDataGridView : System.Windows.Forms.DataGridView

{

public DBDataGridView() { DoubleBuffered = true; }

}

You said you're using a plain DataGrid, rather than a DataGridView. Probably the same principle would apply, especially considering that your stack trace ends up calling DataGridView stuff anyway.

I beleive this is tied to an ancient behavior in windows: if a non-double-buffered object takes too long to redraw, it causes large portions of the screen to be invalidated & redrawn again. Double-buffering prevents this. Perhaps the crash is occuring in large DataGridViews when the second redraw begins & the first one hasn't completed yet Or the first draw deletes some in-use objects while the second redraw is trying to use them I'm not sure, this is just a guess.

As an added benefit, Double-buffering seems to help DataGridViews redraw quicker, or at least cleaner, anyway.

Anyway, hope this helps.

thanks

-Jason






Re: Windows Forms General pretty often: System.AccessViolationException

nobugz

I've seen this stack trace on this forum a few times, GdipDrawLineI() is prone to cause an access violation. Amazingly, this is also reported to happen on Mono, running on Linux. Makes one wonder how much gratuitous source code copying is happening there. The diagnostic for Mono is that the brush that was used to create the pen was disposed. I can see how that would indeed cause a crash.

Note this thread for an example of how this could happen by accident and not seeing the problem until much later.





Re: Windows Forms General pretty often: System.AccessViolationException

FrankSh

Mr. Jason for president!

I had/have this problem in an Outlook AddIn and found out that it only happens when I use a DataGrid or DataGridView on a Winform in an Office AddIn. Windows Forms Applications did not have this problem.

So I was searching for a solution in a completely wrong direction by uninstalling, repairing, reinstalling Office, Framework, SP1 of VS2005 etc

And now, after 2 weeks of no productivity hell I found your solution and it works.

In my 60000 lines AddIn I had to doublebuffer all DataGrids and DatagridViews.

It did not work first; I also had to remove some DoEvents that happened in some Functions that are triggered by a timer.

At least I also had to doublebuffer all NumericUpDown controls because they did not redraw after these updates.

At least now we know its a bug and not a feature and I guess we will not see a fix for that soon because its not really possible to reproduce this error. It just happens on some PCs.

Thanks a lot, Jason

Frank





Re: Windows Forms General pretty often: System.AccessViolationException

SoccerSarah

Haveing a similar problem at build time with visual C#
The "ResolveKeySource" task failed unexpectedly.
System.AccessViolationException: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
at Microsoft.Build.Shared.ExceptionHandling.RethrowUnlessFileIO(Exception e)
at Microsoft.Build.Tasks.ResolveKeySource.ResolveAssemblyKey()
at Microsoft.Build.Tasks.ResolveKeySource.Execute()
at Microsoft.Build.BuildEngine.TaskEngine.ExecuteTask(ExecutionMode howToExecuteTask, Hashtable projectItemsAvailableToTask, BuildPropertyGroup projectPropertiesAvailableToTask, Boolean& taskClassWasFound) PhotometerEquationGenerator

any idea's