【问题标题】:Stack overflow in Designer.csDesigner.cs 中的堆栈溢出
【发布时间】:2016-01-03 00:50:56
【问题描述】:

为我的 winform 自动生成的代码中发生了堆栈溢出。它仅发生在表单的自动生成代码的开头,而不是其中的任何控件。我尝试删除第一行,它发生在下一行。没有堆栈跟踪或内部异常,请帮助。

编辑

这是我的表单代码:

namespace Eternal_Continent
{
    public partial class Almanac : Form
    {
        public Almanac()
        {
            InitializeComponent();
        }
        public List<string> Content = new List<string>();
        private void Almanac_Load(object sender, EventArgs e)
        {
            timer1.Interval = 5000;
            PrivateFontCollection pfc = new PrivateFontCollection();
            pfc.AddFontFile(Application.StartupPath + "\\Resources\\font_name.ttf");
            textBox1.Font = new Font(pfc.Families[0], 36);
        }

        private void Almanac_FormClosing(object sender, FormClosingEventArgs e)
        {
            e.Cancel = true;
            Hide();
        }
    }
}

这是设计师的:

namespace Eternal_Continent
{
    partial class Almanac
    {
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows Form Designer generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.components = new System.ComponentModel.Container();
            System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(Almanac));
            this.textBox1 = new System.Windows.Forms.TextBox();
            this.timer1 = new System.Windows.Forms.Timer(this.components);
            this.SuspendLayout();
            // 
            // textBox1
            // 
            this.textBox1.BackColor = System.Drawing.Color.Khaki;
            this.textBox1.BorderStyle = System.Windows.Forms.BorderStyle.None;
            this.textBox1.Dock = System.Windows.Forms.DockStyle.Fill;
            this.textBox1.Location = new System.Drawing.Point(0, 0);
            this.textBox1.Multiline = true;
            this.textBox1.Name = "textBox1";
            this.textBox1.ReadOnly = true;
            this.textBox1.Size = new System.Drawing.Size(546, 582);
            this.textBox1.TabIndex = 0;
            // 
            // timer1
            // 
            this.timer1.Enabled = true;
            // 
            // Almanac
            // I removed the autoscale lines here, because I wanted to see if it would still create errors, it did
            this.BackgroundImage = Properties.Resources.Stone;
            this.ClientSize = new System.Drawing.Size(546, 582);
            this.Controls.Add(this.textBox1);
            this.Icon = Properties.Textures.EternalContinent1;
            this.MaximizeBox = false;
            this.MinimizeBox = false;
            this.Name = "Almanac";
            this.Text = "Almanac";
            this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(this.Almanac_FormClosing);
            this.Load += new System.EventHandler(this.Almanac_Load);
            this.ResumeLayout(false);
            this.PerformLayout();
            this.Dispose();
        }

        #endregion
        private System.Windows.Forms.Timer timer1;
        public System.Windows.Forms.TextBox textBox1;
    }
}

编辑 #2 删除 Dispose() 行会导致

当前进程已使用其所有系统允许的窗口管理器对象句柄

在我的 Resources.Designer.cs 中。

【问题讨论】:

  • 您是否更改/编辑了设计器代码?
  • 没有一些代码可以参考,我能告诉你的最好的就是:你是否在某处创建了一个无限循环?
  • Plutonix,我这样做是因为我想看看它是否会持续抛出错误,但除此之外,不会。我删除了一行,看看它是否会继续发生。 Paul,据我所知,任何地方都没有循环,它是 VS 自动生成的代码。
  • 保罗的意思是:你有没有创建任何无限递归(调用自身的方法)?请出示您自己和设计者的代码,以便我们了解发生了什么。
  • 我现在上传了代码。

标签: c# winforms


【解决方案1】:

我尝试了您的代码,但遇到了 ObjectDisposed 异常。调试时发现您的自动生成代码的最后一行是this.Dispose(),这是不正确的。

一旦我删除了那行(InitializeComponent() 的最后一行),设计器和代码就可以正常工作,没有任何错误

请注意,我必须注释掉与资源相关的以下行,因为我的项目和文件系统中没有这些行

// Almanac.cs
pfc.AddFontFile(Application.StartupPath + "\\Resources\\font_name.ttf");

// Almanac.Designer.cs
this.BackgroundImage = Properties.Resources.Stone;
this.Icon = Properties.Textures.EternalContinent1;

您还应该从代码中注释掉这些行,以确保您得到与我得到的结果相同的结果,然后一次包含它们以确保它们不会导致任何问题

注意:我在 Win 7 上使用 VS 2013

【讨论】:

  • 谢谢,这解决了。很好,这个错误已经解决了。
【解决方案2】:

这通常归结为偶然的间接递归。我在这里有一个旧答案可能会有所帮助:https://stackoverflow.com/a/4734422/26414

还值得注意的是,如果您逐步使用 F11,您可能会看到告诉您什么是什么的循环模式。请注意您正在调试的线程。如果有多个线程正在执行用户代码,我相信在单步执行时,当前行的高亮显示为深黄色。

【讨论】:

  • 这对我没有帮助,我没有看到任何递归:\
猜你喜欢
  • 2019-05-18
  • 2010-12-26
  • 2016-06-12
  • 2014-12-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-04-15
  • 2012-12-20
相关资源
最近更新 更多