保存文件对话框“有时"抛出 System.AccessViolationException [英] savefiledialog "sometimes" throws an System.AccessViolationException
问题描述
我在 Visual Studio 2013 中使用 Win7
I'm using Win7 with Visual Studio 2013
我的应用程序是一个带有 GeckoFx 的网络浏览器组件.在下载调用时,我触发打开 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:COMPANYproductsAPPLICATIONWITHGecko45MYAPPLICATIONmodMain.vb:Zeile 822.
bei Gecko.LauncherDialog.Show(nsIHelperAppLauncher aLauncher, nsISupports aWindowContext, UInt32 aReason) in D: emp9f0c5cdGeckofx-WinformsDialogsGeckoHelperAppLauncherDialog.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 .. 我不知道为什么会发生这种情况.我刚刚问了另一个问题,为什么会发生这种情况(见这里 SaveFileDialog 在调用后直接自动关闭showDialog())
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 As nsILocalFile
的目的,而且您似乎没有在代码中进一步使用它.这可能是您的记忆遭到破坏的地方,也可能只是我对 GeckoFx 的无知.
On as side note, I'm not sure what your using objTarget As nsILocalFile
for 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.
如果这些都不起作用,您可能需要找出从 System.Windows.Forms.UnsafeNativeMethods.GetSaveFileName(OPENFILENAME_I ofn)
内部获取非托管代码 StackTrace.
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)
.
祝你好运!
这篇关于保存文件对话框“有时"抛出 System.AccessViolationException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!