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

时间:2011-08-29 04:06:47

标签: c# .net winforms debugging stack-overflow

我有一个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(..)正在抛出该错误。

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

更新

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

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

enter image description here

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

还有一些工具提示......

所有实例的{p> Here is a screenshot和单个ToolTip控件图/地图的here is a screenshot ..我不知道如何阅读脸红

2 个答案:

答案 0 :(得分:3)

您的代码存在两个潜在问题:

var toolTip = new ToolTip();

pictureBox.Image = Resources.RedButton;

都在非UI线程上调用。我必须marshal使用Control.Invoke将此代码转换为UI线程。如果解决这个问题没有帮助,请查看我在windows service中如何调试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();
}

值得一读Manipulating Controls from Threads

答案 1 :(得分:1)

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