本文介绍了savefiledialog“有时”抛出System.AccessViolationException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将Win7与Visual Studio 2013一起使用

I'm using Win7 with Visual Studio 2013

我的应用程序是GeckoFx的Web浏览器组件。在下载电话中,我触发打开SaveFileDialog。在某些情况下(并非在每次调用中),当我在第822行调用SaveFileDialog时,出现以下错误(另请参见以下代码):

My Application is a webbrowser-component with GeckoFx. At the download-call I trigger to open the SaveFileDialog. On some cases (not on every call) I got the following error, when I call SaveFileDialog in line 822 (see also code below):

System.AccessViolationException wurde nicht behandelt.
  HResult=-2147467261
  Message=Es wurde versucht, im geschützten Speicher zu lesen oder zu schreiben. Dies ist häufig ein Hinweis darauf, dass anderer Speicher beschädigt ist.
  Source=System.Windows.Forms
  StackTrace:
       bei System.Windows.Forms.UnsafeNativeMethods.GetSaveFileName(OPENFILENAME_I ofn)
       bei System.Windows.Forms.SaveFileDialog.RunFileDialog(OPENFILENAME_I ofn)
       bei System.Windows.Forms.FileDialog.RunDialogOld(IntPtr hWndOwner)
       bei System.Windows.Forms.FileDialog.RunDialog(IntPtr hWndOwner)
       bei System.Windows.Forms.CommonDialog.ShowDialog(IWin32Window owner)
       bei System.Windows.Forms.CommonDialog.ShowDialog()
       bei MYAPPLICATION.modMain.LauncherDialog_Download(Object sender, LauncherDialogEvent e) in X:\COMPANY\products\APPLICATIONWITHGecko45\MYAPPLICATION\modMain.vb:Zeile 822.
       bei Gecko.LauncherDialog.Show(nsIHelperAppLauncher aLauncher, nsISupports aWindowContext, UInt32 aReason) in D:\temp\9f0c5cd\Geckofx-Winforms\Dialogs\GeckoHelperAppLauncherDialog.cs:Zeile 91.
       bei System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
       bei System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
       bei System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
       bei System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
       bei System.Windows.Forms.Application.Run(ApplicationContext context)
       bei Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.OnRun()
       bei Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.DoApplicationModel()
       bei Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.Run(String[] commandLine)
       bei MYAPPLICATION.My.MyApplication.Main(String[] Args) in 17d14f5c-a337-4978-8281-53493378c1071.vb:Zeile 81.
  InnerException:

我已经搜索了这个问题,并照做了saveFileDialog1.AutoUpgradeEnabled = False,正如许多解决方案所建议的那样,但这没有帮助。

I already searched for this problem and took care of saveFileDialog1.AutoUpgradeEnabled = False as many solutions suggests, but it didn't help.

这是出现错误的行。
注意:在某些情况下,我不得不调用SaveFileDialog两次。我不知道为什么会这样。我只是问了另一个为什么发生这种情况的问题(请参见)

Here are the lines in which the error appears.Note: I had to call the SaveFileDialog in some cases twice.. I don't know why this happens. I just asked another question for why this happens (see here SaveFileDialog closes automatically directly after calling showDialog())

Public Sub LauncherDialog_Download(ByVal sender As Object, ByVal e As Gecko.LauncherDialogEvent)

    Try
        Dim P As String = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) & Path.DirectorySeparatorChar & "tmp" 'globalParameters._downloadDirectory '
        If Not System.IO.Directory.Exists(P) Then System.IO.Directory.CreateDirectory(P)

        Dim objTarget As nsILocalFile = Xpcom.CreateInstance(Of nsILocalFile)("@mozilla.org/file/local;1")

        Using tmp As New nsAString(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + vbTab & "temp.tmp")
            objTarget.InitWithPath(tmp)
        End Using

        'Save file dialog
        Dim saveFileDialog1 As New SaveFileDialog()

        saveFileDialog1.Filter = "CSV file (*.csv)|*.csv|All files (*.*)|*.*"
        saveFileDialog1.FilterIndex = 2
        saveFileDialog1.RestoreDirectory = True
        saveFileDialog1.FileName = e.Filename
        saveFileDialog1.AutoUpgradeEnabled = False
        saveFileDialog1.CheckPathExists = False
        saveFileDialog1.InitialDirectory = globalParameters.getDownloadDirectory() 'globalParameters._downloadDirectory

        If globalParameters._doNotShowDownloadPrompt Then
            ' this code Part does not happen in my cases and we can ignore it
        Else
            Dim dialogResultValue As DialogResult
            Try
                dialogResultValue = saveFileDialog1.ShowDialog()
            Catch ex As Exception
                logging.logInformation("Problems during loading of dialog: " & ex.ToString())
            End Try
            ' SpecialCase, if CSV File or Dicom just reopen dialog
            ' crazy but helps to display dialog
            logging.logInformation("Download URL: " & e.Url)
            If (e.Url.Contains("format=CSV") Or e.Url.Contains("DocGenericZIP") Or e.Url.Contains("type=application/dicom")) And dialogResultValue = DialogResult.Cancel Then
                Try
                    saveFileDialog1.Dispose() ' dispose old saveFileDialog

                    saveFileDialog1 = New SaveFileDialog()
                    saveFileDialog1.Filter = "CSV file (*.csv)|*.csv|All files (*.*)|*.*"
                    saveFileDialog1.FilterIndex = 2
                    saveFileDialog1.RestoreDirectory = True
                    saveFileDialog1.FileName = e.Filename
                    saveFileDialog1.AutoUpgradeEnabled = False
                    saveFileDialog1.InitialDirectory = globalParameters.getDownloadDirectory()
                    saveFileDialog1.CheckPathExists = False
                    dialogResultValue = saveFileDialog1.ShowDialog() 'this is the line 822 mentioned in the error above

                Catch ex As Exception
                    logging.logInformation("Errors during loading of fole Dialog: " & ex.ToString())
                End Try
            End If

            ' if upper saveFileDialog runs without errors, following lines works like a charm
            If dialogResultValue = DialogResult.OK Then
                Try
                    ' these lines put the download Information into another thread, so that the download
                    ' can run and the user can use the browser simultaneously,
                    ' otherwise the interaction in main-Form is blocked
                    Dim par As New Parameters
                    par.sender = sender
                    par.e = e
                    par.mime = e.Mime
                    par.url = e.Url
                    par.fileName = saveFileDialog1.FileName
                    par.dialogResultValue = dialogResultValue
                    par.myStream = saveFileDialog1.OpenFile()
                    ThreadJob(par)
                Catch ex As Exception
                    logging.logInformation("Error during loading of File" & e.ToString)
                End Try
            End If
        End If

    Catch ex As Exception
        logging.logInformation("Error during loading file. " & ex.ToString, "main", True)
    Finally
        'nothing to do here
    End Try
End Sub


推荐答案

我查看了您的两个问题,尽管我没有使用GeckoFx的经验,但我将从文件的方式开始在本地创建。您得到一个AccessViolation -2147467261,这通常意味着内存损坏(或至少是锁定的内存)。

I looked at both your questions, and although I have no experience with GeckoFx, I would start with way the file is being created locally. Your getting an AccessViolation -2147467261 which usually means corrupt memory(or at least locked memory).

这可以解释您在其他问题中描述的行为。如果 globalParameters.getDownloadDirectory()中指定的目录本身无效,或者由于引起根问题的原因而损坏,则对话框可能会关闭(这只是推测,因为

This may explain the behavior you described in your other question. If the Directory specified in globalParameters.getDownloadDirectory() is itself invalid, or it became corrupted by whatever is causing your root issue, the Dialog may close(this is just speculation, as it doesn't explain why no error is thrown).

要调试此功能,每次创建目录和/或文件时,我都会记录日志。我还将在整个执行过程中定期检查您是否有权访问要保存到的目录和文件。之所以这样做,是因为 AccessViolation 发生在非托管函数 GetSaveFileName(OPENFILENAME_I ofn)中。这是用于初始化对话框以及我相信在其中设置InitialDirectory的函数。您应该能够看到InitialDirectory和默认文件名。有了这些数据,当它出故障时,您有望看到一个模式。

To debug this, I would log every time you create a directory and/or file. I would also check periodically throughout execution that you have access to the Directory and File you are trying to save to. The reason I would do this is because the AccessViolation is happening in the unmanaged function GetSaveFileName(OPENFILENAME_I ofn). This is the function used to initialize the dialog and where I believe the InitialDirectory is set. You should be able to see the InitialDirectory and default file name. With this data, when it bugs out, you will hopefully see an pattern.

很难更具体一点,因为您没有描述 globalParameters.getDownloadDirectory()的填充方式或更改方式。至此,一个线程提到 .RestoreDirectory 可能是导致此问题的原因,因此请尝试禁用它。您还说过,错误是在第822行引发的,但不包括行号,所以我不知道哪条确切的行在窃听。

It is hard to be more specific as you didn't describe how globalParameters.getDownloadDirectory() is being populated or if it changes. To this point, one thread mentioned that .RestoreDirectory may be the cause of the issue, so maybe try disabling it. You also said that the error is thrown on line 822 but did not include line numbers so I don't know which exact line is bugging out.

在旁边,我不确定您使用 objTarget作为nsILocalFile 的目的是什么,似乎您在代码中没有进一步使用它。

On as side note, I'm not sure what your using objTarget As nsILocalFilefor and it doesn't seem like you use it further in your code. This may be where your memory is being corrupted or it is just my ignorance of GeckoFx.

如果这都不起作用,则可能需要弄清楚非托管代码,这可能是您的内存被破坏的地方,或者仅仅是我对GeckoFx的无知。从 System.Windows.Forms.UnsafeNativeMethods.GetSaveFileName(OPENFILENAME_I ofn)内部。

If none of this works, you may need to figure out to get the unmanaged code StackTrace from inside of System.Windows.Forms.UnsafeNativeMethods.GetSaveFileName(OPENFILENAME_I ofn).

祝你好运!

这篇关于savefiledialog“有时”抛出System.AccessViolationException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 02:11