0
votes

I am developing a C# application in Visual Studio 2008 that uses folder browser dialog and printdialog. Lately the program is encountering an error whenever ShowDialog() Method for these two dialog boxes is called.

The error says:

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

The strange thing is that this error is specific to this project only. When I use folder browser dialog in a new C# project it works perfectly fine. Moreover this error started occurring recently in the mentioned project. Earlier it used to work perfectly fine. Also, this error does not occur when I run the program from the IDE in debugging mode.

There is a suggestion in some other question on Stack Overflow to uncheck "Suppress JIT optimization on module load" in debugging options. I have tried that too but that did not solve my problem.

Can someone please help me solve this problem?

1
Without code, it's highly unlikely anyone will be able to help you. You need to edit your question to incldue minimal (actual) code that will show how you're using the dialogs. You should also search on the error you're getting before posting here; that search shows at least 5 results using the tag [c#] alone; one of them might help. - Ken White
@Ken White, I did read all those threads before posting but none solved my problem. As I said in my question suppressing JIT optimization as suggested in some other thread did not help either. It is not a VS 2008 nor an OS issue (BTW I have windows XP SP3 on the development computer) as other projects work perfectly fine. It only happens in this specific project whenever ShowDialog() methods of Folder Browser Dialog or PrintDialog are called. - Shweta_N
This is an environmental problem, both dialogs are native code. They'll load a bunch of native DLLs into your process. You'll need to get your machine stable again. Think restore point, that sort of fix. - Hans Passant

1 Answers

0
votes

It's difficult to say what without seeing any code. I've seen running into this issue with accessing UI on threads other than it was created. To avoid it, if you're messing with UI controls from some thread, you can create a function like the following

public static void ExecuteAction(Control myControl, Action myAction) 
{
    if (myControl.InvokeRequired) { myControl.Invoke(myAction); }
    else { myAction(); }
}

Then you just call that from whatever thread you want with the control and the action you want to do (say, change its color or what).

I haven't seen the issue with Dialogs specifically since I haven't done anything with them in a long time, but since it's a UI issue, and it's the same error message I've seen before, I'd give it a shot.
You might also check the value of Control.CheckForIllegalCrossThreadCalls. http://msdn.microsoft.com/en-us/library/system.windows.forms.control.checkforillegalcrossthreadcalls.aspx