3

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)

Jon
  • 5,247
  • 6
  • 30
  • 38
  • Does this ever happen without the Adobe control? – John Saunders Jul 26 '09 at 18:27
  • No - and 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). – Jon Jul 26 '09 at 18:36
  • 1
    This sounds like an Adobe bug. You may want to ask them. – John Saunders Jul 26 '09 at 18:38

2 Answers2

3

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
  • 72,686
  • 18
  • 132
  • 173
  • 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 Jul 26 '09 at 19:06
1

I had the same problem and worked around it by using the PreMessageFilter on the form or user control. This enables you to intercept the tab button press on the Adobe control to avoid the control throwing the exception. This might not be a brilliant but if you want to use the Adobe PDF viewer which most users have seen and used before then this will give you a work around and stop your application crashing.

The PreMessageFilter is documented on MSDN - http://msdn.microsoft.com/en-us/library/system.windows.forms.application.addmessagefilter(v=vs.110).aspx

I have also put up a VB.Net example of this you can download. - http://msdeveloper.co.uk/net/item/102-adobe-viewer-for-winforms

I would be interested to see if anyone else has encountered this problem or has any other solution.