如何在WinForm应用程序中调试此StackOverflowException?

我有一个winform应用程序。 每隔几秒我检查一些日志文件,读入任何新数据并将任何新数据插入数据库。

当我运行应用程序大约一个小时1/2时,我得到一个StackOverflowException 。 整个期间的日志文件中没有新数据,因此没有新的数据添加到数据库中。

代码在这里出错…

 if (pictureBox == null) { continue; } if (pictureBox.InvokeRequired) { var toolTip = new ToolTip(); GameServer tempGameFile = gameServer; pictureBox.Invoke(new MethodInvoker( () => toolTip.SetToolTip(pictureBox, string.Format( "{0} : Last Checked: {1}; Last Updated: {2}", tempGameFile.Name, tempGameFile.CheckedOn.ToLongTimeString(), tempGameFile.UpdatedOn.HasValue ? tempGameFile.UpdatedOn.Value.ToLongTimeString() : "-No Date Set-")))); } pictureBox.Image = Resources.RedButton; 

pictureBox.Invoke(..)抛出该错误。

那么..我不知道我怎么能想到这一点来弄清楚发生了什么? 有什么建议?

UPDATE

尝试Dmitry的建议我已经启动了一个ANTS探测器内存配置文件..并快速浏览一下……似乎有很多ToolTip控件的实例。

这是20分钟后的class级列表摘要。

在此处输入图像描述

很多EventHandlers(我不发布什么东西?)

还有一些工具提示……

这是所有实例的屏幕截图 , 这里是一个 ToolTip控件图/地图的屏幕截图 ..我不知道如何阅读腮红

您的代码有2个潜在问题:

var toolTip = new ToolTip();

pictureBox.Image = Resources.RedButton;

都是在非UI线程上调用的。 我必须使用Control.Invoke将此代码封送到UI线程。 如果解决这个问题没有帮助,请查看我在Windows服务中如何调试StackOverflowException的答案。

更新:尝试此代码。 请注意,每个引用任何UI控件的语句都需要使用Control.Invoke进行封送处理:

 if (pictureBox == null || !pictureBox.IsHandleCreated) { continue; } Action setTooltipAndImage = () => { var toolTip = new ToolTip(); GameServer tempGameFile = gameServer; toolTip.SetToolTip(pictureBox, string.Format(...)); pictureBox.Image = Resources.RedButton; }; if (pictureBox.InvokeRequired) { pictureBox.Invoke(setTooltipAndImage); } else { setTooltipAndImage(); } 

可能值得阅读从线程操纵控件 。

如果您可以在调试模式下运行应用程序,当您点击StackOverflowException并且应用程序进入visual studio时,打开调用堆栈窗口(Debug – > Windows – > Call Stack)并查看导致您的代码的原因抛出exception。