2

A stack overflow is occurring in the auto-generated code for my winform. It happens only at the start of the auto-generated code for the form, not any of the controls in it. I tried removing the first line, and it happened on the next one. There is no stack trace or inner exception, please help.

EDIT

Here is my code for the form:

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();
        }
    }
}

And here is the designer's:

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;
    }
}

EDIT #2 Removing the Dispose() line causes

The current process has used all of its system allowance of handles for Window Manager objects

in my Resources.Designer.cs.

  • 1
    Did you alter/edit the designer code? – Ňɏssa Pøngjǣrdenlarp Jan 03 '16 at 00:55
  • without some code to reference, the best I can throw at you is this: did you create an endless loop somewhere? – Paul Jan 03 '16 at 00:56
  • Plutonix, I did because I wanted to see if it would continually throw the error, but other than that, no. I removed one line, to see if it would keep occuring. Paul, as far as I know, there are no loops anywhere, It's auto generated code by VS. –  Jan 03 '16 at 00:57
  • What Paul meant was: did you create any endless recursion (method that calls itself)? Please show the code, your own and the designer's, so we can get an idea of what happened. – René Vogt Jan 03 '16 at 01:02
  • I uploaded the code now. –  Jan 03 '16 at 01:07
  • Not sure why there's a `this.Dispose` added but try removing that line. It's just before the close brace of the `InitializeComponent` method. Generally you want to avoid manual changes in `designer.cs` as it'll get often get overwritten. – keyboardP Jan 03 '16 at 01:24
  • I removed that line, now I'm getting "The current process has used all of its system allowance of handles for Window Manager objects" in my resources.designer file. Should I start a new question? –  Jan 03 '16 at 01:33
  • Possibly or you could update this question and mention that without the `Dispose` line, you get that new error message. I've not really encountered this before but it seems there must be something else going on in code elsewhere. I tried running your example in a new project and it works fine (although I commented out `this.Icon = Properties.Textures.EternalContinent1;` and `this.BackgroundImage = Properties.Resources.Stone;`) as I don't have those resources. – keyboardP Jan 03 '16 at 01:36
  • Regarding the new error, this thread might help https://social.msdn.microsoft.com/Forums/windows/en-US/73aaa1f3-30a7-4593-b299-7ec1fd582b27/the-current-process-has-used-all-of-its-system-allowance-of-handles-for-window-manager-objects?forum=winforms – keyboardP Jan 03 '16 at 01:37
  • Since this code looks fine and combining the information from the two error scenarios: is it possible that you accidently recurse the instantiation of `new Almanac ()`? Does the error only occur at runtime or also in design mode? – René Vogt Jan 03 '16 at 02:02

2 Answers2

0

Often this will be down to accidental, indirect recursion. I have an old answer here that might help: https://stackoverflow.com/a/4734422/26414

It's also worth noting that if you step through using F11 you might see the looping pattern that tells you what's what. Be aware of which thread you're debugging, too. I believe you get a darker yellow highlight for the current line when stepping-through, if there are multiple threads with user code executing.

Community
  • 1
  • 1
Neil Barnwell
  • 41,080
  • 29
  • 148
  • 220
0

I tried your code and I got an ObjectDisposed exception. On debugging it turns out that your Auto-generated code has the last line as this.Dispose(), which is incorrect.

Once I deleted that line (last line of InitializeComponent() ) the designer and the code worked fine without any errors

Note that I had to comment out the below lines related to the resources since I do not have that in my project & file-system

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

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

You should also comment out these lines from your code to make sure you get the same results as I get, and then include them one at a time to make sure they are not causing any issues

NOTE: I am using VS 2013 on Win 7

Vikhram
  • 4,294
  • 1
  • 20
  • 32
  • Thanks, this fixed it. It's good that this error has been resolved. –  Jan 04 '16 at 04:56