C# OpenFileDialog 中的 Stackoverflow/CLR 错误
下面的代码是我的悲伤示例之一(三个)。这是一个简单的 OpenFileDialog() 调用,会导致程序崩溃。在 XP 上,如果对话框保持打开状态几秒钟,就会发生崩溃。在 Vista 上,如果用户选择“我的电脑”,则会发生崩溃。在VS2008中,调试器有时会捕获stackoverflowException。如果我在第一行(新...)中放置一个断点,vshost.exe 就会崩溃。如果我在 ShowDialog() 行放置一个断点,则会收到 FatalExecutionEngineError。如果我在没有 vshost 的情况下编译,应用程序将运行直到随机崩溃(就像在 XP 上一样 - 有一定的时间)。
还有另外两个打开的对话框可以打开不同类型的文件,这三个对话框都具有相同的行为。类似的代码在我的其他项目中没有表现出相同的行为。
线程公寓是单身。我尝试设置 ValidateNames = false。在大多数情况下,调试器都会陷入困境。
OpenFileDialog imageDlg = new OpenFileDialog();
imageDlg.Filter = "All Images|*.jpg;*.jpeg;*.png;*.tif;*.tiff;*.bmp|All Files|*.*|JPEGs (*.jpg)|*.jpg|PNGs (*.png)|*.png|TIFFs (*.tiff)|*.tiff|TIFFs (*.tif)|*.tif|BMPS (*.bmp)|*.bmp";
imageDlg.Title = "Select Scan Image";
if (DialogResult.OK == imageDlg.ShowDialog())
{
updateImageDisplay();
}
事件处理程序代码:
//
// setScratchImageButton
//
this.setScratchImageButton.Location = new System.Drawing.Point(191, 15);
this.setScratchImageButton.Name = "setScratchImageButton";
this.setScratchImageButton.Size = new System.Drawing.Size(26, 23);
this.setScratchImageButton.TabIndex = 8;
this.setScratchImageButton.Text = "...";
this.setScratchImageButton.UseVisualStyleBackColor = true;
this.setScratchImageButton.Click += new System.EventHandler(this.setScratchImageButton_Click);
调用的代码
private void updateImageDisplay()
{
if (null != project.srcImage)
{
imageDisplay.SizeMode = PictureBoxSizeMode.Normal;
if (project.srcImage != null)
{
imageDisplay.ClientSize = new Size(project.srcImage.Width, project.srcImage.Height);
imageDisplay.Image = (Image)project.srcImage;
}
this.ScratchImage.Text = project.srcImageLocation;
}
else
{
imageDisplay.Image = null;
this.ScratchImage.Text = "";
}
ImageDisplayPanel.Refresh();
}
The code below is one (of three) examples of my grief. It is a simple OpenFileDialog() call which causes the program to crash. On XP, the crash occurs if the dialog stays open for several seconds. On Vista, the crash occurs if the user selects "My Computer". In VS2008, the debugger sometimes catches a stackoverflowexception. If I put a break point in the first line (new ...), vshost.exe crashes. If I put a break point at the ShowDialog() line, I get a FatalExecutionEngineError. If I compile without vshost, the application will run until a random crash (as on XP - there is some amount of time).
There are two other open dialogs that open different types of files, all three of which have the same behavior. Similar code does not show the same behavior in my other projects.
The thread apartment is single. I have tried setting ValidateNames = false. The debugger is falling off the deep-end in most cases.
OpenFileDialog imageDlg = new OpenFileDialog();
imageDlg.Filter = "All Images|*.jpg;*.jpeg;*.png;*.tif;*.tiff;*.bmp|All Files|*.*|JPEGs (*.jpg)|*.jpg|PNGs (*.png)|*.png|TIFFs (*.tiff)|*.tiff|TIFFs (*.tif)|*.tif|BMPS (*.bmp)|*.bmp";
imageDlg.Title = "Select Scan Image";
if (DialogResult.OK == imageDlg.ShowDialog())
{
updateImageDisplay();
}
Event handler code:
//
// setScratchImageButton
//
this.setScratchImageButton.Location = new System.Drawing.Point(191, 15);
this.setScratchImageButton.Name = "setScratchImageButton";
this.setScratchImageButton.Size = new System.Drawing.Size(26, 23);
this.setScratchImageButton.TabIndex = 8;
this.setScratchImageButton.Text = "...";
this.setScratchImageButton.UseVisualStyleBackColor = true;
this.setScratchImageButton.Click += new System.EventHandler(this.setScratchImageButton_Click);
Code called
private void updateImageDisplay()
{
if (null != project.srcImage)
{
imageDisplay.SizeMode = PictureBoxSizeMode.Normal;
if (project.srcImage != null)
{
imageDisplay.ClientSize = new Size(project.srcImage.Width, project.srcImage.Height);
imageDisplay.Image = (Image)project.srcImage;
}
this.ScratchImage.Text = project.srcImageLocation;
}
else
{
imageDisplay.Image = null;
this.ScratchImage.Text = "";
}
ImageDisplayPanel.Refresh();
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
什么情况下调用显示该对话框的方法?此错误最可能的来源是多次生成该事件并导致向用户显示许多
OpenFileDialog
实例。它们可能会显示在彼此之上,从而仅显示一个对话框。编辑
如果只是调试器场景失败,则尝试关闭调试器属性窗口中的隐式函数计算(工具 -> 选项 -> 调试器)。通过调试器查看时,表单上的属性之一可能会导致堆栈溢出。
Under what circumstances is the method which displays this dialog being called? The most likely source of this error is that the event is being generated many times and causing many instances of
OpenFileDialog
to be displayed to the user. They are potentially being displayed on top of each other giving the appearance of only a single dialog.EDIT
If it's only the debugger scenario that is failing then try turning off implicit function evaluation into debugger property windows (Tools -> Options -> Debugger). It's possible one of the properties on your form is causing a stack overflow when viewed through the debugger.
我添加到项目中的 DLL 导致堆损坏。症状是奇怪而美丽的崩溃。
A DLL I had added to the project was causing heap corruption. The symptom was strange and beautiful crashes.