Stackoverflow/CLR Error in C# OpenFileDialog
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();
}
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.
A DLL I had added to the project was causing heap corruption. The symptom was strange and beautiful crashes.
精彩评论