473,487 Members | 2,474 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Random AccessViolationExceptions

VS2008 .net 3.5 WinForms application contains only managed code.
At random places my application causes exception below.

env: Vista SP1.

How to fix ?

Andrus.
System.AccessViolationException was unhandled
Message="Attempted to read or write protected memory. This is often an
indication that other memory is corrupt."
Source="System.Windows.Forms"
StackTrace:
at System.Windows.Forms.UnsafeNativeMethods.CallWindo wProc(IntPtr
wndProc, IntPtr hWnd, Int32 msg, IntPtr wParam, IntPtr lParam)
at System.Windows.Forms.NativeWindow.DefWndProc(Messa ge& m)
at System.Windows.Forms.ToolTip.WndProc(Message& msg)
at System.Windows.Forms.ToolTip.ToolTipNativeWindow.W ndProc(Message&
m)
at System.Windows.Forms.NativeWindow.DebuggableCallba ck(IntPtr hWnd,
Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.UnsafeNativeMethods.DispatchM essageW(MSG&
msg)
at
System.Windows.Forms.Application.ComponentManager. System.Windows.Forms.UnsafeNativeMethods.IMsoCompo nentManager.FPushMessageLoop(Int32
dwComponentID, Int32 reason, Int32 pvLoopData)
at
System.Windows.Forms.Application.ThreadContext.Run MessageLoopInner(Int32
reason, ApplicationContext context)
at
System.Windows.Forms.Application.ThreadContext.Run MessageLoop(Int32 reason,
ApplicationContext context)
at System.Windows.Forms.Application.Run(Form mainForm)
....

Jul 13 '08 #1
7 3530
Andrus wrote:
VS2008 .net 3.5 WinForms application contains only managed code.
At random places my application causes exception below.

env: Vista SP1.

How to fix ?

Andrus.
System.AccessViolationException was unhandled
Message="Attempted to read or write protected memory. This is often an
indication that other memory is corrupt."
Source="System.Windows.Forms"
StackTrace:
at System.Windows.Forms.UnsafeNativeMethods.CallWindo wProc(IntPtr
wndProc, IntPtr hWnd, Int32 msg, IntPtr wParam, IntPtr lParam)
If some window proc is causing an access violation, then either there's a
bug in the window proc code itself (which is possible but unlikely if it's
pure managed code) or another application is subclassing windows or messing
with the dispatch logic improperly. A popular mistake is subclassing a
window with a dialog procedure pointing in a DLL, then having the DLL
unloaded somehow, resulting in a call to invalid memory.

Reboot in safe mode and see if the problem disappears. Or less drastically,
exit all programs that aren't necessary (including background programs) and
see if the problem disappears. You can use the "autoruns" tool
(http://technet.microsoft.com/sysinternals/bb963902) to get a list of
automatically starting programs and easily disable them.

If it's a third-party app, some trial and error should point you to the
culprit. If it's really a bug in the framework, good luck, these are a
little harder to diagnose and fix (or work around, as the case may be).

For problems like these, it pays to have a clean installation of Windows in
a virtual machine that you can use to verify that your application runs
correctly there. If it doesn't, you have to focus your efforts on that; if
it does, you know that the cause of failure is most likely something added
after a clean install, and you just have to find out what.

--
J.
Jul 13 '08 #2
If some window proc is causing an access violation, then either there's a
bug in the window proc code itself (which is possible but unlikely if it's
pure managed code) or another application is subclassing windows or
messing with the dispatch logic improperly. A popular mistake is
subclassing a window with a dialog procedure pointing in a DLL, then
having the DLL unloaded somehow, resulting in a call to invalid memory.
I use class WaitOper below in many places like

using (WaitOper waitOper = new WaitOper()) {
TimeConsumingOperation();
}

Can this usage cause this exception ?
class WaitOper : IDisposable
{
Cursor saveCursor;
internal WaitOper()
{
saveCursor = Cursor.Current;
Cursor.Current = Cursors.WaitCursor;
}

public void Dispose()
{
Cursor.Current = saveCursor;
GC.SuppressFinalize(this); // added since FxCop recommends
this.
}
}

Reboot in safe mode and see if the problem disappears. Or less
drastically, exit all programs that aren't necessary (including background
programs) and see if the problem disappears.
I do'nt have exact steps to reproduce so it is difficult to verify it it
disappers.

Andrus.

Jul 14 '08 #3
Andrus wrote:
>If some window proc is causing an access violation, then either
there's a bug in the window proc code itself (which is possible but
unlikely if it's pure managed code) or another application is
subclassing windows or messing with the dispatch logic improperly. A
popular mistake is subclassing a window with a dialog procedure
pointing in a DLL, then having the DLL unloaded somehow, resulting in
a call to invalid memory.

I use class WaitOper below in many places like

using (WaitOper waitOper = new WaitOper()) {
TimeConsumingOperation();
}

Can this usage cause this exception ?
No. Managed code cannot (or should not be able to) cause access violations,
unless you use P/Invoke or unsafe operations, or the framework itself
contains a bug (this is rare but unfortunately not unheard of, and can be
damn annoying).
class WaitOper : IDisposable
{
Cursor saveCursor;
internal WaitOper()
{
saveCursor = Cursor.Current;
Cursor.Current = Cursors.WaitCursor;
}

public void Dispose()
{
Cursor.Current = saveCursor;
GC.SuppressFinalize(this); // added since FxCop recommends
this.
Your class doesn't have a finalizer, so you can remove the need for
..SuppressFinalize() by declaring your class sealed. If you know there will
be no subclasses to introduce a finalizer, you don't need to call
..SuppressFinalize() (FxCop will detect this).

Also, your class is already "internal" by default, so there's no need for
declaring the constructor internal. Declaring it public is less mysterious.

Aside from these nitpicks, this code shouldn't be causing you problems,
unless there's some subtle problem with cursors that I'm not aware of. Of
course, if you want to be sure, you can always stub out the class to see if
it helps.
>Reboot in safe mode and see if the problem disappears. Or less
drastically, exit all programs that aren't necessary (including
background programs) and see if the problem disappears.

I do'nt have exact steps to reproduce so it is difficult to verify it it
disappers.
That's always the hard part. There are several tools that may be of help.
One is AppVerifier (http://technet.microsoft.com/library/bb457063), which
may spot some problems an unseen unmanaged part of your application may be
causing. Failing that, you can always go for the all-out debugging option
with the Debugging Tools for Windows
(http://www.microsoft.com/whdc/devtools/debugging/) but there's a pretty
steep learning curve to the world of unmanaged debugging. Still, in catching
the access violation it might give you a good hint where the problem might be.

--
J.
Jul 14 '08 #4
>>If some window proc is causing an access violation, then either there's
>>a bug in the window proc code itself (which is possible but unlikely if
it's pure managed code) or another application is subclassing windows or
messing with the dispatch logic improperly. A popular mistake is
subclassing a window with a dialog procedure pointing in a DLL, then
having the DLL unloaded somehow, resulting in a call to invalid memory.
I have managed dlls only. .NET does not allow to unload assemblies. How to
unload dll ?
Also, your class is already "internal" by default, so there's no need for
declaring the constructor internal. Declaring it public is less
mysterious.
If I remove internal keyword from constructor, a lot of

.... is inaccessible due to its protection level

compile errors occur since constructor becomes private. I use it from main
appl exe file only, so public
constructor is not required.
So I do'nt understand how it is reasonable to declare constructor not
internal.
That's always the hard part. There are several tools that may be of help.
One is AppVerifier (http://technet.microsoft.com/library/bb457063), which
may spot some problems an unseen unmanaged part of your application may be
causing.
Application does not start under AppVerifier. Vista shows:

myapp.exe stops working

and Cancel button.

AppVerifier shows message on save:

The tests you have selected for [myapp.exe] require a debugger and points to
page
http://www.microsoft.com/whdc/devtoo...g/default.mspx

How I can use AppVerifier ?
Failing that, you can always go for the all-out debugging option
with the Debugging Tools for Windows
(http://www.microsoft.com/whdc/devtools/debugging/) but there's a pretty
steep learning curve to the world of unmanaged debugging. Still, in
catching the access violation it might give you a good hint where the
problem might be.
What should I install something from there to allow AppVerifier to work or
can I exclude some AppVerifier tests ?

I override ProcessCmdKey() in number of classes to implement custom keys in
winforms.

[SecurityPermission(SecurityAction.LinkDemand,
Flags = SecurityPermissionFlag.UnmanagedCode)]
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)

FxCop orders to add UnmamagedCode flag for this method in base classes. Can
this cause this exception ?

Andrus.

Jul 15 '08 #5
Andrus wrote:
>>>If some window proc is causing an access violation, then either there's
a bug in the window proc code itself (which is possible but unlikely if
it's pure managed code) or another application is subclassing
windows or
messing with the dispatch logic improperly. A popular mistake is
subclassing a window with a dialog procedure pointing in a DLL, then
having the DLL unloaded somehow, resulting in a call to invalid memory.

I have managed dlls only. .NET does not allow to unload assemblies. How to
unload dll ?
Unmanaged DLLs could be injected into your application by any number of
components. You cannot unload these, as you cannot specifically check for
them, and even if you could, it wouldn't be safe. The only way to avoid
problems with an injected DLL is to make sure it doesn't get to inject
itself in the first place, hence the recommendation for testing it in safe mode.
>Also, your class is already "internal" by default, so there's no need for
declaring the constructor internal. Declaring it public is less
mysterious.

If I remove internal keyword from constructor, a lot of

... is inaccessible due to its protection level

compile errors occur since constructor becomes private. I use it from main
appl exe file only, so public
constructor is not required.
So I do'nt understand how it is reasonable to declare constructor not
internal.
What I mean is this: because you've already declared the class itself as
"internal", the constructor is not accessible to clients outside the
assembly to begin with, as they can't even see the class. It's not *wrong*
to declare the constructor internal, but it's not *necessary* either. If you
ever decide to reuse the class and package it in a library, for example (in
other words, if you ever make the class public) the constructor will have to
be changed too.

To me, "internal" strongly implies "this member is tightly coupled with the
rest of the assembly". The class may be, but the constructor in particular
isn't.
>That's always the hard part. There are several tools that may be of help.
One is AppVerifier (http://technet.microsoft.com/library/bb457063), which
may spot some problems an unseen unmanaged part of your application
may be
causing.

Application does not start under AppVerifier. Vista shows:

myapp.exe stops working

and Cancel button.
That's moderately positive, as it could mean it's detected a problem.
AppVerifier shows message on save:

The tests you have selected for [myapp.exe] require a debugger and
points to
page
http://www.microsoft.com/whdc/devtoo...g/default.mspx

How I can use AppVerifier ?
Start AppVerifier, add the application to the list of applications running
under AppVerifier. You can now exit AppVerifier.

Starting the application under a debugger is as simple as debugging it from
Visual Studio (this works if you point AppVerifier to the .exe in your
bin\Debug directory). You may need to check "enable unmanaged debugging" in
the project properties for this to work, though.

Visual Studio may not report all problems when using AppVerifier; the native
debuggers may be a better option.
>Failing that, you can always go for the all-out debugging option
with the Debugging Tools for Windows
(http://www.microsoft.com/whdc/devtools/debugging/) but there's a pretty
steep learning curve to the world of unmanaged debugging. Still, in
catching the access violation it might give you a good hint where the
problem might be.

What should I install something from there to allow AppVerifier to work or
can I exclude some AppVerifier tests ?
You really want to run the application under a debugger, whether it's Visual
Studio or the Debugging Tools for Windows. For one thing, the access
violation you're trying to diagnose is an exception that can only be
properly diagnosed under a debugger too.

The instructions for installation and use of the native debuggers are on the
page linked to.
I override ProcessCmdKey() in number of classes to implement custom keys in
winforms.

[SecurityPermission(SecurityAction.LinkDemand,
Flags = SecurityPermissionFlag.UnmanagedCode)]
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)

FxCop orders to add UnmamagedCode flag for this method in base classes. Can
this cause this exception ?
Not the flag itself. You can cause problems if you actually call unmanaged
or unsafe code in your override, or you modify the "msg" in such a way that
it's no longer valid (because one of the parameters holds a pointer, for
example).

--
J.
Jul 15 '08 #6
Unmanaged DLLs could be injected into your application by any number of
components. You cannot unload these, as you cannot specifically check for
them, and even if you could, it wouldn't be safe. The only way to avoid
problems with an injected DLL is to make sure it doesn't get to inject
itself in the first place, hence the recommendation for testing it in safe
mode.
I don't understand this: inject itself.
Does this mean that dll can load itself a second time ? How this can cause
access violation ?
How testing is safe mode can diagnose this ?
What I mean is this: because you've already declared the class itself as
"internal", the constructor is not accessible to clients outside the
assembly to begin with, as they can't even see the class. It's not *wrong*
to declare the constructor internal, but it's not *necessary* either. If
you ever decide to reuse the class and package it in a library, for
example (in other words, if you ever make the class public) the
constructor will have to be changed too.
I understand from this that marking internal class members internal instead
of public is not good practice.
If so, why compiler and FxCop does not report this ?
Starting the application under a debugger is as simple as debugging it
from Visual Studio (this works if you point AppVerifier to the .exe in
your bin\Debug directory). You may need to check "enable unmanaged
debugging" in the project properties for this to work, though.
I use C# Express 2008. I havent found "enable unmanaged debugging" checkbox
in project properties.
You really want to run the application under a debugger, whether it's
Visual Studio or the Debugging Tools for Windows. For one thing, the
access violation you're trying to diagnose is an exception that can only
be properly diagnosed under a debugger too.
I installed latest Debugging Tools and ran application under AppVerifier
using

"C:\Program Files\Debugging Tools for Windows (x86)\ntsd" myapp.exe

with Basic checkbox selected.
File Exit command in application causes breakpoint. ntsd window shows:

ModLoad: 75260000 752a5000 C:\Windows\system32\bcrypt.dll
ModLoad: 6ba40000 6ba5c000 SMDiagnostics.dll
ModLoad: 08680000 0869c000 SMDiagnostics.dll
ModLoad: 6ba40000 6ba5c000
C:\Windows\assembly\GAC_MSIL\SMDiagnostics\3.0.0.0 _
_b77a5c561934e089\SMDiagnostics.dll
ModLoad: 6d930000 6d938000 MyApp.Resource.dll
ModLoad: 12250000 12258000 MyApp.Resource.dll
ModLoad: 6d930000 6d938000
I:\raamat\MyApp\MyApp\bin\Debug\MyApp.Resource.dll
ModLoad: 6d870000 6d878000 image6d870000
ModLoad: 12260000 12268000 image12260000
ModLoad: 6d870000 6d878000
I:\raamat\MyApp\MyApp\bin\Debug\en\MyApp.Resource. reso
urces.dll
ModLoad: 6b010000 6b016000 C:\Windows\system32\dciman32.dll
ModLoad: 74e30000 74e51000 C:\Windows\system32\NTMARTA.DLL
ModLoad: 755c0000 755d1000 C:\Windows\system32\SAMLIB.dll
(1f4.1468): Invalid handle - code c0000008 (first chance)
First chance exceptions are reported before any exception handling.
This exception may be expected and handled.
eax=c0000008 ebx=00000000 ecx=0723f37c edx=772b9a38 esi=00000044
edi=00000000
eip=772b9a6f esp=0723f328 ebp=0723f378 iopl=0 nv up ei pl nz na pe
nc
cs=001b ss=0023 ds=0023 es=0023 fs=003b gs=0000
efl=00000206
ntdll!KiRaiseUserExceptionDispatcher+0x37:
772b9a6f 8b0424 mov eax,dword ptr [esp]
ss:0023:0723f328=c0000008
0:002>

App Verifier shows log below.
If I check all checkboxes in AppVerifier, exception occurs at starting.
ntsd also issues warning at startup:

Symbol search path is: *** Invalid ***
************************************************** **************************
* Symbol loading may be unreliable without a symbol search path. *
* Use .symfix to have the debugger choose a symbol path. *
* After setting your symbol path, use .reload to refresh symbol locations. *
************************************************** **************************
Executable search path is:
ModLoad: 10f10000 10f64000 MyApp.exe
ModLoad: 77260000 77387000 ntdll.dll
ModLoad: 6a220000 6a251000 C:\Windows\system32\verifier.dll
Page heap: pid 0x149C: page heap enabled with flags 0x3.
AVRF: eeva.exe: pid 0x149C: flags 0x80000181: application verifier enabled
ModLoad: 6d900000 6d928000 C:\Windows\system32\vrfcore.dll
ModLoad: 6d880000 6d8b9000 C:\Windows\system32\vfbasics.dll
ModLoad: 79000000 79046000 C:\Windows\system32\mscoree.dll
ModLoad: 76c30000 76d0b000 C:\Windows\system32\KERNEL32.dll
(149c.120c): Break instruction exception - code 80000003 (first chance)
eax=00000000 ebx=00000000 ecx=0023fa94 edx=772b9a94 esi=fffffffe
edi=772bb6f8
eip=772a7dfe esp=0023faac ebp=0023fadc iopl=0 nv up ei pl zr na pe
nc
cs=001b ss=0023 ds=0023 es=0023 fs=003b gs=0000
efl=00000246
*** ERROR: Symbol file could not be found. Defaulted to export symbols for
ntdl
l.dll -
ntdll!DbgBreakPoint:
772a7dfe cc int 3
0:000>

however current (Debug) directory contains pdb files created by C# Express.

How to diagnose problem from this infomation ?
Not the flag itself. You can cause problems if you actually call unmanaged
or unsafe code in your override, or you modify the "msg" in such a way
that it's no longer valid (because one of the parameters holds a pointer,
for example).
My method does not change msg parameter.
So I can assume that this cannnot cause access violation.

Andrus.
<?xml version="1.0" encoding="UTF-8" standalone="no" ?>
- <avrf:logfile xmlns:avrf="Application Verifier">
- <avrf:logSession TimeStarted="2008-07-16 : 11:38:54" PID="5208"
Version="2">
- <avrf:logEntry Time="2008-07-16 : 11:39:04" LayerName="Handles"
StopCode="0x300" Severity="Error">
<avrf:message>Invalid handle exception for current stack
trace.</avrf:message>
<avrf:parameter1>c0000008 - Exception code.</avrf:parameter1>
<avrf:parameter2>6e7f178 - Exception record. Use .exr to display
it.</avrf:parameter2>
<avrf:parameter3>6e7f18c - Context record. Use .cxr to display
it.</avrf:parameter3>
<avrf:parameter4>0 - Not used.</avrf:parameter4>
- <avrf:stackTrace>
<avrf:trace>vfbasics!VfBasicsStopMessage+d1</avrf:trace>
<avrf:trace>vfbasics!AVrfpVectoredExceptionHandler +9b</avrf:trace>
<avrf:trace>ntdll!RtlGetProcessHeaps+13f</avrf:trace>
<avrf:trace>ntdll!RtlInitializeCriticalSectionAndS pinCount+164</avrf:trace>
<avrf:trace>ntdll!RtlInitializeCriticalSectionAndS pinCount+1d5</avrf:trace>
<avrf:trace>ntdll!KiUserExceptionDispatcher+f</avrf:trace>
<avrf:trace>ntdll!KiFastSystemCallRet+0</avrf:trace>
<avrf:trace>WS2_32!WSAUnhookBlockingHook+ada</avrf:trace>
<avrf:trace>WS2_32!send+4a</avrf:trace>
</avrf:stackTrace>
</avrf:logEntry>
</avrf:logSession>
</avrf:logfile>

Jul 16 '08 #7
Also,

http://technet.microsoft.com/en-us/l.../bb457063.aspx

contains

If you are not comfortable using a debugger, you can run the tests by
selecting the Use the appverifier debugger to get crash logs and log kernel
checks option.

I havent found such option in AppVerifier .
So I do'nt understand how to select this option.

Andrus.

Jul 16 '08 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

28
3651
by: Paul Rubin | last post by:
http://www.nightsong.com/phr/python/sharandom.c This is intended to be less predicable/have fewer correlations than the default Mersenne Twister or Wichmann-Hill generators. Comments are...
10
2481
by: Virus | last post by:
Ok well what I am trying to do is have 1.) the background color to change randomly with 5 different colors.(change on page load) 2,) 10 different quotes randomly fadeing in and out in random...
10
5948
by: Johnny Snead | last post by:
Hey guys, Need help with this random sort algorithm private void cmdQuestion_Click(object sender, System.EventArgs e) { Random rnd = new Random(); //initialize rnd to new random object...
10
2484
by: Marshall Belew | last post by:
I'm trying to synchronize a network app that uses random numbers generated by System.Random. Rather than pass every randomly generated number, I just pass the seed. I'm seeing a result that leads...
15
2941
by: Steven Macintyre | last post by:
Hi all, I need to retrieve an integer from within a range ... this works ... below is my out puts ... it just does not seem so random ... Is there perhaps a suggestion out there to create a...
1
338
by: FAQ server | last post by:
----------------------------------------------------------------------- FAQ Topic - How do I generate a random integer from 1 to N?...
8
3893
by: Daniel | last post by:
Hey guys Using Random(), how random is it, is it possible to be predicted? I have a requirement for a very good random number generator. I was doing something such as: Random randomSeed = new...
6
4791
by: Mike Langworthy | last post by:
i can not seem to get this code to work. someone please help using System; using System.Collections.Generic; using System.Text; namespace ConsoleApplication1 { class Program {
0
854
by: Andy | last post by:
Hi, I'm coding some new BOs, and I've hit a problem when using one of my pre-existing libraries. The only changes is that I've installed SP1 for .Net 3.5 and VS 2008. My older DAL that I...
0
7105
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7180
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6846
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5439
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4870
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4564
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3071
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1381
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
600
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.