savefiledialog“有时”会抛出System.AccessViolationException

时间:2017-04-04 13:46:17

标签: vb.net access-violation savefiledialog geckofx

我在Visual Studio 2013中使用Win7

我的应用程序是GeckoFx的webbrowser组件。在下载调用时,我触发打开SaveFileDialog。在某些情况下(不是每次调用),当我在第822行调用SaveFileDialog时,我得到以下错误(参见下面的代码):

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正如许多解决方案所暗示的那样,但它并没有帮助。

以下是出现错误的行。 注意:在某些情况下,我不得不两次调用SaveFileDialog ..我不知道为什么会这样。我刚刚问了另一个问题,为什么会发生这种情况(见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

1 个答案:

答案 0 :(得分:1)

我看了你的两个问题,虽然我没有GeckoFx的经验,但我会先从本地创建文件开始。您获得AccessViolation -2147467261通常意味着内存损坏(或至少锁定内存)。

这可以解释您在其他问题中描述的行为。如果<input class="genericButton" id="login-password" type="password" name ="password" placeholder="Password"> 中指定的目录本身无效,或者由于导致根问题而导致其损坏,则对话框可能会关闭(这只是推测,因为它不能解释为什么不会抛出错误)。 / p>

要调试它,我会在每次创建目录和/或文件时进行记录。我还会在整个执行过程中定期检查您是否可以访问要保存的目录和文件。我这样做的原因是因为.genericButton { font-family: 'Roboto'; font-weight: 300; background-color: rgba(50,50,50,0.3); color: aliceblue; transition: 0.25s; width: 190px; font-size: 1em; } 发生在非托管函数globalParameters.getDownloadDirectory()中。这是用于初始化对话框的函数以及我认为设置了InitialDirectory的位置。您应该能够看到InitialDirectory和默认文件名。有了这些数据,当它出错时,你会希望看到一个模式。

RunFileDialog

GetSaveFileName

OPENFILENAME

很难更具体,因为你没有描述如何填充AccessViolation或者它是如何变化的。到目前为止,一个帖子提到GetSaveFileName(OPENFILENAME_I ofn)可能是问题的原因,所以可能尝试禁用它。您还说错误是在第822行引发的,但不包括行号,因此我不知道哪条行确实在线。

另请注意,我不确定您使用globalParameters.getDownloadDirectory()的内容是什么,而且您似乎没有在代码中进一步使用它。这可能是你的记忆被破坏的地方,或者只是我对GeckoFx的无知。

如果这些都不起作用,您可能需要弄清楚从.RestoreDirectory内部获取非托管代码StackTrace。

General AccessViolation TroubleShooting

-2147467261 Error

-2147467261 Error

祝你好运!