Mike Martin24365

Ok I have a project that runs fine from the IDE, the problem is if I install the app or run it from the bin\release folder then I get this error on one of my method calls. I have tried this on a test machine and on my development machine and the results are the same. I have spent hours trying to figure this one out....... The crazy thing is, the method shows up in the stack trace but there is not code at all executed in that method.

Re: Visual C# General Wierd error : Attempted to read or write protected memory. This is often an indication that other memory is corrupt

ivolved

 Mike Martin wrote:
Ok I have a project that runs fine from the IDE, the problem is if I install the app or run it from the bin\release folder then I get this error on one of my method calls. I have tried this on a test machine and on my development machine and the results are the same. I have spent hours trying to figure this one out....... The crazy thing is, the method shows up in the stack trace but there is not code at all executed in that method.

This shouldn't happen in managed code. It's even odder that the error only happens when not debugging. Can you give the code for the method that causes the problem

Michael






Re: Visual C# General Wierd error : Attempted to read or write protected memory. This is often an indication that other memory is corrupt

RCroft

I get the exact same error on startup of MS Outlook w/ business contact manager. In my quest to find out what was causing it, I stumbled upon your post. No other apps running, sys is Dell XPS 2 laptop w/ MS office 2003. The problem makes Outlook unusable. I'm guessing the program or system is ordering the executing of a write command faster than the memory is released

Please note that I know very little about the inner working of software, and I'm sure it shows!

Best of luck,

RC




Re: Visual C# General Wierd error : Attempted to read or write protected memory. This is often an indication that other memory is corrupt

DomiS

I've got the same problem with my own developed Programm!
It runs fine when pressing the "Start Debugging"-Button in Visual Studio 2005, but this error occurs, when I run it from command line.
Besides VS 2005 Retail (no Beta!) the .NET framework 2.0 is installed, and the system is up to date (all updates from update.microsoft.com).

In my programm I've got two Event-Handler running side by side in their own threads. One Event-Handler gets active when there is data on the serial port (using System.IO.Ports) and the other is a SNMP-Trap-Handler which I've rewritten in VB.NET from a C#-example. This SNMP-Trap-Handler uses a .NET-interoperability-class which is needed to use the unmanaged code that the wsnmp32.dll from windows provides!

The SNMP-Event-handler, which I have rewritten in VB.NET, alone does run perfect, just like when the combinated programm is startet within Visual Studio, as I said before.

It seems to me, that the programm always aborts, when the SNMP-Handler gets active. If I just cause some serial data events, the programm runs just fine, even when started from command-line.
So I would expect the error within SNMP-Trap-Handler code, if asked!
But why does the programm run fine from within Visual Studio I have no idea!!!


Here is a link to that guy who developed the interoperability class und also the C#-snmp-trap-handler: http://dotnet.sys-con.com/read/38936.htm



Perhaps just one small update:
I have tested the programm with the original C# snmp-handler just to see if I made a mistake when rewriting it in VB.NET, but the problem is the same here.

I compiled the original trap-handler.cs file to a dll. A added this dll as a refernce to my other programm which handles the serial events and runs the main.
When I debug this in Visual Studio the programm runs fine again, just like it is supposed to. But when I start it from command line, I have the same problem:

Unhandled Exception: System.AccessViolationException: Attempted to read or write
 protected memory. This is often an indication that other memory is corrupt.


I have no doubt what's going wrong!
What's the difference between running from command line and starting it from within Visual Studio




Re: Visual C# General Wierd error : Attempted to read or write protected memory. This is often an indication that other memory is corrupt

Mike Martin

ok, just an update on what I found. I had the following in a method

x=(variableA+variableB+ anothermathfunction(variableC,variableD));

what I changed that fixed my problem:

y= anothermathfunction(variableC,variableD);

x=(variableA+variableB+ y);

Now I cant explain this but after HOURS of trial and error this fixed my problem.

Good Luck......





Re: Visual C# General Wierd error : Attempted to read or write protected memory. This is often an indication that other memory is corrupt

ivolved

Mike Martin wrote:

ok, just an update on what I found. I had the following in a method

x=(variableA+variableB+ anothermathfunction(variableC,variableD));

what I changed that fixed my problem:

y= anothermathfunction(variableC,variableD);

x=(variableA+variableB+ y);

Now I cant explain this but after HOURS of trial and error this fixed my problem.

Good Luck......

Odd. Is your application multi-threaded What does anothermathfunction do






Re: Visual C# General Wierd error : Attempted to read or write protected memory. This is often an indication that other memory is corrupt

DomiS

Hello again,

unfortunatelly your solution didn't help me with my problem, but it lead me to my own solution.
I told a colleague what you posted and he suggested, that those problems might be caused by bad compiler optimizations!
So I searched Visual Studio and found out that you can turn off compiler optimizations in "Properties/Compile/Advancec Compile Options" of the program.

Now my program seems to run fine, thanks! :)




Re: Visual C# General Wierd error : Attempted to read or write protected memory. This is often an indication that other memory is corrupt

DejanR

You should set you Debugger in VS2005:

Tools->Options

Debugging->General

uncheck option "Suppress JIT optimization on module load"





Re: Visual C# General Wierd error : Attempted to read or write protected memory. This is often an indication that other memory is corrupt

DomiS

Thanks, I will try that!




Re: Visual C# General Wierd error : Attempted to read or write protected memory. This is often an indication that other memory is corrupt

Drew Noakes

Thank you for your suggestion, however this didn't help me in my case.

I'm seeing this exception when calling an extern method on a COM DLL.

It's still early-days for my debugging, but it's quite frustrating. This worked fine with VS 2003, and I'm only seeing it now I try to upgrade.

Fingers crossed!




Re: Visual C# General Wierd error : Attempted to read or write protected memory. This is often an indication that other memory is corrupt

Sossles

I had the exact same message when running my company's application, written in VS2003, on a new machine with only VS2005 installed. When I installed the .NET v1.1 runtime, the problem went away. I also saw it on another application, in that case I had to actually provide a config file for the EXE to make it run inside the v1.1 runtime, but again that fixed it.

Steve.





Re: Visual C# General Wierd error : Attempted to read or write protected memory. This is often an indication that other memory is corrupt

krhyzsy

i have a very close similiraty with your prob..and am not getting any solutions yet..if u have already resolved yours, i would very much appreciate it if you post it here..so that i may also refer:)..thanx




Re: Visual C# General Wierd error : Attempted to read or write protected memory. This is often an indication that other memory is corrupt

krhyzsy

DejanR wrote:

You should set you Debugger in VS2005:

Tools->Options

Debugging->General

uncheck option "Suppress JIT optimization on module load"

am using VS2005 and i can't seem to follow this navigations..or are u missing something






Re: Visual C# General Wierd error : Attempted to read or write protected memory. This is often an indication that other memory is corrupt

Matthew Watson

I can see that option ok (using VS2005).




Re: Visual C# General Wierd error : Attempted to read or write protected memory. This is often an indication that other memory is corrupt

Wardog

I was having the same problem when trying to instantiate a FoxPro .dll in my VS2005.net web application on the production server. My problem was in the COM .dll itself using a variable GETENV("TEMP").

The problem is that the web app was running as a user via the IIS Application Pooling and the GETENV("TEMP") variable was pointing to an environment variable that didn't exist (bad path)

My advice to you is to search your COM .dll with a fine-toothed comb and look at variables/paths that dont jive from client side testing to production server testing.

Good Luck!