views:

4201

answers:

14

I have a program that uses the built in webbrowser control. At some point during the usage of this, I'm not sure at what point, but it appears to be random, I get the following error:

System.AccessViolationException

FullText = System.AccessViolationException: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason, Int32 pvLoopData)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.Run(Form mainForm)

Does anyone have any clues as to why I would get this and how to prevent it?

+1  A: 

My gut feeling is that you are trying to manipulate the document before you've navigated to one. Try navigating to "about:blank" before changing the document text or html.

If you already are performing navigation, note that navigation is asynchronous, so you need to monitor the events of the browser in order to detect when the navigation is complete. Otherwise, you may try to write to the document before it exists.

Jeff Yates
I'm not writing to the document. I'm just passing a URL to the webbrowser. I also have a back and a refresh button. I'm not manipulating the text or html in the document at any time.
Aaron Smith
+1  A: 

We're hitting this too. Inconsistently, we'll get this exception.

Some questions to help narrow this down: are you using any mshtml interfaces directly (e.g. mshtml.dll)? Doing any COM interop directly?

We've found that calling some of the COM MSHTML interfaces incorrectly can cause this.

We've also found that doing COM marshalling incorrectly can cause this.

If there's a bug in the MSHTML interface import that the built-in WebBrowser uses, it can cause this.

Accessing document IFRAME Elements from another domain can cause this.

It's possible that making WebBrowser calls when the document isn't quite ready may also cause this.

Judah Himango
A: 

Just a suggestion, I'm no expert on this but have used the WebBrowser a lot in previous applications, but why dont you write a function to wait 1 second before attempting to pass the browser anything and always check the readystate beforehand aswell. Might slow it down a bit but it should make it bullet proof. :)

Kevin Dark
I'll try it and see if it works.
Aaron Smith
A: 

Are the pages you are navigating to hosting any ActiveX controls? If yes, one of those may be flawed. Also check your pages in IE. See if they crash the same way. That will help isolate if it's specific to the content or the browser control.

Bob King
A: 

I have the same problem, with a page showing an ActiveX we have written. If I view the exact same page in IE7.0.6 the problem doesn't occur.

Is there a way to debug this, so we can narrow down the fault the ActiveX may cause (its written by another team, so I have no control on this).

Thanks, Alex

A: 

I forgot to mention that everything is fine on Windows XP and that the problem only occurs on Vista (I have SP1, but I don't know if its the same on Vista without SP1)

Thanks, Alex

we are seeing the problem on XP machines as well.
Judah Himango
+2  A: 

The content of this article worked for me, I hope it will do the same for all of you.

http://jtstroup.net/CommentView,guid,3fa30293-a3a4-4a1c-a612-058e751ad151.aspx

Alex

The users that are crashing with my app are using Windows XP.
Aaron Smith
Mine too, but this might fix it. We'll give this a try.
Judah Himango
New URL for the same webpage: http://jtstroup.net/post/Attempted-to-read-or-write-protected-memory-This-is-often-an-indication-that-other-memory-is-corrupt.aspx
Eyal
A: 

We're having this too... but the web browser is just loading a couple of normal web pages? Anyone come up with a solution?

A: 

Same problem, the exception occured because of the function Webbrowser Navigate. I've tryed the solution of jstroup.net but it's still here. I used to have the exception when I navigate on gamespot.com Forget to say : Windows XP, VS 2008

A: 

Hi, I have got the same problem with a VB express 2008 and Webbrowser. The site which hangs up attempt to load Windows Media Player. (http://www.spa.asso.fr/) The code of this page seems to be involved but is there any way to trap the exception ?

A: 

I ended up just opening up the webpage in the browser. That way I don't even have to worry about this. It's still strange that it throws this error though.

Aaron Smith
I originally accepted this, but some people may not be able to use this as an answer, so I'm going to leave this question unanswered for now as long as other people are having issues.
Aaron Smith
A: 

This seems to be a Vista Issue, what happend to me was that my C# webBrowser1 opend a web page that runned a java applet that opend a external IE webpage that runs a ActiveX app/script.

When the ActiveX script tryes to update back in to the memory of the C# app the DEP "Data Execution Prevention" in Vista flags this operation as hostile/virus and ends the program with the System.AccessViolationException: Attempted to read or write protected memory. This is often an indication that other memory is corrupt."

My fix for this was to turn of DEP in Vista with this line in cmd

"bcdedit.exe /set {current} nx AlwaysOff"

and reboot the machine.

XP also run DEP so in certain cases i guess this cold happen here too. To test if its a DEP issue do this.

Right click on "My Computer" Select "Properties" and "Advanced" Under "Startup and Recovery, click Settings Now click on "Edit" The notepad has just begun. Simply replace the line: Code: noexecute optionn by AlwaysOff Restart your PC to complete the transaction.

If you want to reactivate the DEP be sufficient to conduct the reverse, like this:

Replace by Quote: AlwaysOff noexecute = noexecute = optin

Darkmage
This was happening on XP machines as well.
Aaron Smith
XP too runs DEP so i wold try to turn of DEP while debugging to see if this is the issue.
Darkmage
A: 

I have this exact same exception. I have a winform app that hosts a webbrowser control and repeatedly browses to a list of urls checking to see how long it takes them to load. The application will usally run for days at a time before the error occurs. I'm also using holding a reference to the webbrowsers underlying activeX instance so that I can use it to assign the NavigateError handler so I can catch http errors on page loads. I've also noticed a steady increase in memory usage from that process in the task manager for the duration of the run. I'm running on server 2008.

Has anyone gotten any more insight to this problem?

cyrix86