473,799 Members | 3,026 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

AccessViolation Exception: Attempted to read or write protected memory...

Hi,

My C# app throws the following exception:

System.AccessVi olationExceptio n: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.

From what I read so far I found that the problem can be connected to P/Invoke and that the places at which this exception is thrown might not reflect the actual problem. In my case, the exception is thrown, when I try to minimise my main window to the system tray and to hide the window from the taskbar:

public void Minimize()
{
this.WindowStat e = FormWindowState .Minimized;
this.ShowInTask bar = false; // Exception!
this.Visible = false;
this.exit = true;
}

Here is more detail:

1. I removed all code that deals with P/Invoke, all DLLImports and so on to make sure I am using only fully managed code.

2. I turned off any virus scanner. This could be one possible source of the problem.

3. My configuration is: VS 2005 (SP1 and Vista update), C#, .NET 2, Vista Business.

4. Even though my app contains code to make a web service call, such code will not be called in the scenario that causes the exception. I read that remoting can be a source of the problem.

5. I can reproduce the problem on my machine. Below is a Stack Trace and a Call Stack output. It starts in an OnClosing event that I connect to and then follows a call to the Minimize method before leaping into .NET with ShowInTaskbar (the idea is to cancel the closing of the form in this scenario, to minimize it and to show a tray icon). What I find odd, is the fact that the TreeView Control is involved in the .NET code and that a TreeNode is set in this stack trace. An implementation of TreeView.afterS elect shows that this event will indeed be fired while ShowInTaskbar has been called. I mention this because in the scenario, in which the exception is thrown, I have to expand a few tree node and to send the app to the system tray one or two times until the exception is thrown.

Any idea what could cause the exception?

Thanks and best wishes,

Marc.

Stack Trace from Exception dialog:

" at System.Windows. Forms.UnsafeNat iveMethods.Call WindowProc(IntP tr wndProc, IntPtr hWnd, Int32 msg, IntPtr wParam, IntPtr lParam)\r\n at System.Windows. Forms.NativeWin dow.DefWndProc( Message& m)"

Call Stack:

System.Windows. Forms.dll!Syste m.Windows.Forms .NativeWindow.D efWndProc(ref System.Windows. Forms.Message m = {msg=0x110b hwnd=0x1110704 wparam=0x9
lparam=0x3fb6cf 0 result=0x0}) + 0x94 bytes
System.Windows. Forms.dll!Syste m.Windows.Forms .Control.DefWnd Proc(ref System.Windows. Forms.Message m) + 0xc bytes
System.Windows. Forms.dll!Syste m.Windows.Forms .Control.WndPro c(ref System.Windows. Forms.Message m) + 0x90b bytes
System.Windows. Forms.dll!Syste m.Windows.Forms .TreeView.WndPr oc(ref System.Windows. Forms.Message m) + 0xedf bytes
System.Windows. Forms.dll!Syste m.Windows.Forms .Control.Contro lNativeWindow.O nMessage(ref System.Windows. Forms.Message m) + 0xd bytes
System.Windows. Forms.dll!Syste m.Windows.Forms .Control.Contro lNativeWindow.W ndProc(ref System.Windows. Forms.Message m) + 0xd6 bytes
System.Windows. Forms.dll!Syste m.Windows.Forms .NativeWindow.D ebuggableCallba ck(System.IntPt r hWnd, int msg = 4363, System.IntPtr wparam, System.IntPtr lparam) + 0x75 bytes
[Native to Managed Transition]
[Managed to Native Transition]
System.Windows. Forms.dll!Syste m.Windows.Forms .Control.SendMe ssage(int msg, int wparam, System.IntPtr lparam) + 0x44 bytes
System.Windows. Forms.dll!Syste m.Windows.Forms .TreeView.Selec tedNode.set(Sys tem.Windows.For ms.TreeNode value) + 0x74 bytes
System.Windows. Forms.dll!Syste m.Windows.Forms .TreeView.OnHan dleCreated(Syst em.EventArgs e) + 0x331 bytes
System.Windows. Forms.dll!Syste m.Windows.Forms .Control.WmCrea te(ref System.Windows. Forms.Message m) + 0x40 bytes
System.Windows. Forms.dll!Syste m.Windows.Forms .Control.WndPro c(ref System.Windows. Forms.Message m) + 0x447 bytes
System.Windows. Forms.dll!Syste m.Windows.Forms .TreeView.WndPr oc(ref System.Windows. Forms.Message m) + 0xedf bytes
System.Windows. Forms.dll!Syste m.Windows.Forms .Control.Contro lNativeWindow.O nMessage(ref System.Windows. Forms.Message m) + 0xd bytes
System.Windows. Forms.dll!Syste m.Windows.Forms .Control.Contro lNativeWindow.W ndProc(ref System.Windows. Forms.Message m) + 0xd6 bytes
System.Windows. Forms.dll!Syste m.Windows.Forms .NativeWindow.D ebuggableCallba ck(System.IntPt r hWnd, int msg = 1, System.IntPtr wparam, System.IntPtr lparam) + 0x75 bytes
[Native to Managed Transition]
[Managed to Native Transition]
System.Windows. Forms.dll!Syste m.Windows.Forms .UnsafeNativeMe thods.CreateWin dowEx(int dwExStyle, string lpszClassName, string lpszWindowName, int style, int
x, int y, int width, int height,
System.Runtime. InteropServices .HandleRef hWndParent,
System.Runtime. InteropServices .HandleRef hMenu,
System.Runtime. InteropServices .HandleRef hInst, object pvParam) + 0x45 bytes
System.Windows. Forms.dll!Syste m.Windows.Forms .NativeWindow.C reateHandle(Sys tem.Windows.For ms.CreateParams
cp) + 0x241 bytes
System.Windows. Forms.dll!Syste m.Windows.Forms .Control.Create Handle() + 0x16a bytes
System.Windows. Forms.dll!Syste m.Windows.Forms .TreeView.Creat eHandle() + 0x6c bytes
System.Windows. Forms.dll!Syste m.Windows.Forms .Control.Recrea teHandleCore() + 0x11b bytes
System.Windows. Forms.dll!Syste m.Windows.Forms .Control.OnPare ntHandleRecreat ed() + 0xe4 bytes
System.Windows. Forms.dll!Syste m.Windows.Forms .Control.Recrea teHandleCore() + 0x207 bytes
System.Windows. Forms.dll!Syste m.Windows.Forms .Control.OnPare ntHandleRecreat ed() + 0xe4 bytes
System.Windows. Forms.dll!Syste m.Windows.Forms .Control.Recrea teHandleCore() + 0x207 bytes
System.Windows. Forms.dll!Syste m.Windows.Forms .Control.OnPare ntHandleRecreat ed() + 0xe4 bytes
System.Windows. Forms.dll!Syste m.Windows.Forms .Control.Recrea teHandleCore() + 0x207 bytes
System.Windows. Forms.dll!Syste m.Windows.Forms .Control.OnPare ntHandleRecreat ed() + 0xe4 bytes
System.Windows. Forms.dll!Syste m.Windows.Forms .Control.Recrea teHandleCore() + 0x207 bytes
System.Windows. Forms.dll!Syste m.Windows.Forms .Control.OnPare ntHandleRecreat ed() + 0xe4 bytes
System.Windows. Forms.dll!Syste m.Windows.Forms .Control.Recrea teHandleCore() + 0x207 bytes
System.Windows. Forms.dll!Syste m.Windows.Forms .Control.OnPare ntHandleRecreat ed() + 0xe4 bytes
System.Windows. Forms.dll!Syste m.Windows.Forms .Control.Recrea teHandleCore() + 0x207 bytes
System.Windows. Forms.dll!Syste m.Windows.Forms .Form.RecreateH andleCore() + 0x25e bytes
System.Windows. Forms.dll!Syste m.Windows.Forms .Form.ShowInTas kbar.set(bool value) + 0xdc bytes
backup.exe!Adon ec.Backup.MainF orm.Minimize() Line 392 + 0x9 bytes C#
backup.exe!Adon ec.Backup.MainF orm.OnClosing(S ystem.Component Model.CancelEve ntArgs e = {System.Windows .Forms.FormClos ingEventArgs}) Line 367 + 0x7 bytes C#

Aug 6 '07 #1
1 4116
Marc Bartsch schrieb:
Hi,

My C# app throws the following exception:

System.AccessVi olationExceptio n: Attempted to read or write protected
memory. This is often an indication that other memory is corrupt.

From what I read so far I found that the problem can be connected to
P/Invoke and that the places at which this exception is thrown might not
reflect the actual problem. In my case, the exception is thrown, when I
try to minimise my main window to the system tray and to hide the window
from the taskbar:
After further tests, I found out that disabling Visual Styles in my application seems to get rid of the problem:

[STAThread]
static void Main(string[] args)
{
//Application.Ena bleVisualStyles ();
Application.Set CompatibleTextR enderingDefault (false);
MainForm mf = new MainForm();

Application.Run (mf);
}

Without visual styles I cannot reproduce the exception anymore. With visual styles, it is reproducable in Debug and Release configuration. This may not be the actual problem and disabling visual styles may only mask the real problem, but this exception seems very hard to track down.

Does anyone know whether visual styles could have something to do with an AccessViolation Exception?

Best wishes,

Marc.
Aug 7 '07 #2

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

Similar topics

2
11324
by: James Arnold | last post by:
I am getting the following error message in my WinForms project: --- 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.
2
20746
by: adypoly | last post by:
Hi guys... I am having a typical problem in using one of the native dll in C# I'll explain what am trying to do, I've a dll written in C language which i am trying to include in my C# project, the solutions builds fine but once i try to run the program it gives an exception as "Attempted to read or write protected memory. This is often an indication that other memory is corrupt" I am passing two integer pointer variables as the...
1
2417
by: =?Utf-8?B?SmFzb24gUm9kbWFu?= | last post by:
I have a wierd problem in my application where any call to Assembly.GetFullName of the currently running assembly fails with an AccessViolationException. This only happens on windows vista, all editions. Running the appliction as an administrator does not seem to help. Any idea what would cause this? Does this have to do with code access security, or assembly signing? Here is a trace of one exception: System.TypeInitializationException:...
12
3550
by: rmiller407 | last post by:
I am getting an AccessViolationException when calling an old legacy fortran dll from c# in vs2008 express, but the same code worked fine in vs2005. If I create a vs2005 c# wrapper dll to call the old fortran dll, then I get the exception if I call that wrapper dll from a vs2008 console app, but not if I call it from a vs2005 console app. I've tried changing the vs2008 code to target the 2.0 framework, but I still get the exception. ...
5
3076
by: Danny Ni | last post by:
Hi, I need help with the following exceptions, it happens to my dev machine and live server every now and then, most of the times they happen after I recompile the solution on my dev machine or deploy the new assembly to live server. TIA Attempted to read or write protected memory. This is often an indication that other memory is corrupt. Description: An unhandled exception occurred during the execution of the
2
6482
by: mcfly1204 | last post by:
Attempted to read or write protected memory. This is often an indication that other memory is corrupt. I have looked into the issue with varied results. With ASP.NET applications, most people can resolve this issue by deleting the solution file and opening the project. Though this is a C#.NET project, I tried the same, but the issue persists. Below is the call stack; I am at a loss with this one.
3
6949
by: sriram347 | last post by:
Hi I am a newbie to ASP.NET. I developed a web page (project type is web application) and I keep getting this error. B]Error message : "System.AccessViolation Exception attempted to read or write protected memory. this is often an indication that other memory is corrupt.... " These are all the platform info : Visual Studio 2005 (VB.NET) and ASP.NET 2.0.
5
10886
by: craig1231 | last post by:
I have a problem trying to pass a structure to an unmanaged c++ DLL from C#. When I call PCSBSpecifyPilotLogon from C#, it throws an AccessViolationException This is what I have... The exported C++ DLL Function; __declspec(dllexport) void PCSBSpecifyATCLogon(PCSBSessionID, const char * inServer, unsigned short inPort, const char * inID, const char * inPassword, const PCSBATCConnection_t * inInfo); And the C++ structure;typedef...
3
5898
by: maneshborase | last post by:
Hi friends, I am facing one serious problem in my application. I am trying to open dicom image file (.dcm) has size around 400 MB. But I am getting and unhandy exceptions, Some time, {System.AccessViolationException: Attempted to read or write protected memory. This is often an indication that other memory is corrupt. at...
0
9546
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10491
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10268
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10247
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9079
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7571
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6809
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5593
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3762
muto222
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.