Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# WebBrowser Control System.AccessViolationException

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?

like image 908
Aaron Smith Avatar asked Oct 07 '08 15:10

Aaron Smith


3 Answers

We have recently had a similar problem on the machines of several customers. The problem turned out to be a bug in the MSHTML control in certain environments. A common symptom for the problem seems to be the broken registration of the jscript.dll library.

Symptoms that may help to diagnose if it's the same problem - the jscript.dll is not listed in Modules in the debugger and is not loaded by the process; Native stack trace for the crash is the following:

mshtml!CRootTracker::CollectGarbageInternal+0xd
mshtml!CDoc::ReduceMemoryPressureTask+0x29
mshtml!CStackPtrAry<unsigned long,12>::GetStackSize+0xb6
mshtml!GlobalWndProc+0x183
USER32!InternalCallWinProc+0x23
USER32!UserCallWinProcCheckWow+0x109
USER32!DispatchMessageWorker+0x3bc
USER32!DispatchMessageW+0xf

The solution is to re-register the jscript.dll library and the crash should go away.

Re-registering the library is done as follows (example given for 64-bit Windows, otherwise only the first line is necessary):

C:\Windows\System32\regsvr32.exe C:\Windows\System32\jscript.dll
C:\Windows\SysWOW64\regsvr32.exe C:\Windows\SysWOW64\jscript.dll

Both commands have to be "Run as Administrator".

like image 107
Filip Navara Avatar answered Oct 17 '22 04:10

Filip Navara


I encountered this exception at various occasions while trying to access WebBrowser.ReadyState and WebBrowser.Document.

I was having the exceptions exclusively on Windows XP 32bit. After the other solutions didn't help, it appeared to be a threading issue. I surrounded any code blocks that accessed the web browser control with mutex locks, and that seemed to solve the problem.

like image 4
Jacotb Avatar answered Oct 17 '22 03:10

Jacotb


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.

like image 3
Jeff Yates Avatar answered Oct 17 '22 04:10

Jeff Yates