views:

477

answers:

1

I have a very simple Windows application, with a few controls, a DataGridView and the Adobe PDF viewer control. The app works fine except when tabbing around to switch focus between controls, I regularly get the following exception.

System.AccessViolationException was unhandled
  Message="Attempted to read or write protected memory. This is often an indication that other memory is corrupt."
  Source="System.Windows.Forms"
  StackTrace:
       at System.Windows.Forms.UnsafeNativeMethods.CallWindowProc(IntPtr wndProc, IntPtr hWnd, Int32 msg, IntPtr wParam, IntPtr lParam)
       at System.Windows.Forms.NativeWindow.DefWndProc(Message& m)
       at System.Windows.Forms.Control.DefWndProc(Message& m)
       at System.Windows.Forms.Control.WndProc(Message& m)
       at System.Windows.Forms.AxHost.WndProc(Message& m)
       at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
       at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
       at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
       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)
       at Scan_Manager.Program.Main() in D:\Checkouts\Code\Scan Manager\Scan Manager\Program.cs:line 17
       at System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)
       at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
       at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ThreadHelper.ThreadStart()
  InnerException:

Anyone point me in the right direction?

Thanks, Jon

Update

After playing around some more, it only happens when the Adobe control loses focus. I can get around somewhat by disabling its Tabstop, but sadly when I use LoadFile to load a PDF it gets focus automatically (and then the app crashes when I tab off)

+2  A: 

This is probably not going to be helpful, but I've only seen those exceptions when I was either using a third party COM component (like an Adobe Acrobat Reader) or when I've been doing something that requires a LOT of memory.

This is one of those errors that is very difficult to troubleshoot, and answers to this tend to vary based on what is causing the issue.

This may be a stretch, but if you're hosting an Acrobat Reader directly in your application, you could try instead displaying the document using the WebBrowser control and setting the DocumentSource to the location of the PDF. The WebBrowser control is, I believe (although I'm not sure) based on Internet Explorer, and it's possible that Internet Explorer does a better job of hosting these controls than a pure .Net WinForms application. I've had some success showing other files this way rather than trying to include a third party COM reference. If that does work, please let me know.

David Stratton
Using the WebBrowser control has indeed fixed this, thanks! There are focus issues with the Adobe reader...but that's another story (and probably question).
Jon