【问题标题】:Program crashes on calling InitializeComponent()调用 InitializeComponent() 时程序崩溃
【发布时间】:2013-07-12 15:12:40
【问题描述】:

好的。这个有点小菜鸟。

我正在为 Access 数据库编写前端代码(我知道,这是在重新发明轮子)。这个前端很简单。个人可以添加新条目或搜索现有条目。两者都工作正常。该程序是自我更新的,虽然非常简单。它比较已执行程序的当前文件版本(不是程序集),并用远程驱动器版本引用自身以查看哪个更新。如果远程驱动器的可执行文件较新,我会生成一个批处理文件来更新脚本,方法是杀死当前程序,将较新的程序复制到与旧程序相同的目录,然后自动打开新程序。每次用户打开程序时都会进行此更新检查。

我不确定这是否相关,但我最近一直在编辑该过程以适用于 XP 和 7。因此,我需要对其进行修改以容纳文件路径中的空格(文档和设置 - XP)。这一切都很好,花花公子。

不知从何而来(似乎),该程序开始在我的测试 XP 机器上持续崩溃(在 2 台不同的 XP 机器上验证)。我的个人机器正在运行 7,并且完全按照预期运行,包括更新,一切正常。

XP 上的崩溃是在我的 Home 表单的 InitializeComponent() 中未处理的异常。我的表单的快速布局如下:

  • BaseForm.cs - 主窗体/父窗体。 .exe 时启动的第一个表单 打开。没有GUI,只存在调用其他孩子。
  • Home.cs - 孩子 BaseForm 的。包含主页 GUI 和按钮以调用其他形式,如 添加/搜索条目。

我们不会进入其他表单,因为它会在加载 Home.cs 表单之前崩溃。

我已将其范围缩小为在 Home 的 InitializeComponent() 中崩溃,在以下构造函数中:

public Home(string DB)
    {
        InitializeComponent();
        database = DB;
        lblVersion.Text = "Version: " + FileVersionInfo.GetVersionInfo(Directory.GetCurrentDirectory() + "\\KnowledgeBase.exe").ProductVersion;
    }

并带有以下消息。抱歉,颗粒感...屏幕截图: 我的代表少于 10 个,并且不会输入错误:

这是我的 InitializeComponent()

private void InitializeComponent()
    {
        System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(Home));
        this.btnNewEntry = new System.Windows.Forms.Button();
        this.label1 = new System.Windows.Forms.Label();
        this.label2 = new System.Windows.Forms.Label();
        this.btnSearch = new System.Windows.Forms.Button();
        this.lblVersion = new System.Windows.Forms.Label();
        this.linklblChangelog = new System.Windows.Forms.LinkLabel();
        this.button1 = new System.Windows.Forms.Button();
        this.pictureBox1 = new System.Windows.Forms.PictureBox();
        ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit();
        this.SuspendLayout();
        // 
        // btnNewEntry
        // 
        this.btnNewEntry.Font = new System.Drawing.Font("Microsoft Sans Serif", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
        this.btnNewEntry.Location = new System.Drawing.Point(391, 407);
        this.btnNewEntry.Name = "btnNewEntry";
        this.btnNewEntry.Size = new System.Drawing.Size(142, 43);
        this.btnNewEntry.TabIndex = 0;
        this.btnNewEntry.Text = "Add new entry";
        this.btnNewEntry.UseVisualStyleBackColor = true;
        this.btnNewEntry.Click += new System.EventHandler(this.btnNewEntry_Click);
        // 
        // label1
        // 
        this.label1.AutoSize = true;
        this.label1.Font = new System.Drawing.Font("Microsoft Sans Serif", 20F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
        this.label1.Location = new System.Drawing.Point(184, 9);
        this.label1.Name = "label1";
        this.label1.Size = new System.Drawing.Size(349, 31);
        this.label1.TabIndex = 1;
        this.label1.Text = "Nationwide Knowledgebase";
        // 
        // label2
        // 
        this.label2.AutoSize = true;
        this.label2.Font = new System.Drawing.Font("Microsoft Sans Serif", 16F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
        this.label2.Location = new System.Drawing.Point(280, 41);
        this.label2.Name = "label2";
        this.label2.Size = new System.Drawing.Size(147, 26);
        this.label2.TabIndex = 2;
        this.label2.Text = "RADS Edition";
        // 
        // btnSearch
        // 
        this.btnSearch.Font = new System.Drawing.Font("Microsoft Sans Serif", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
        this.btnSearch.Location = new System.Drawing.Point(190, 407);
        this.btnSearch.Name = "btnSearch";
        this.btnSearch.Size = new System.Drawing.Size(142, 43);
        this.btnSearch.TabIndex = 3;
        this.btnSearch.Text = "Search database";
        this.btnSearch.UseVisualStyleBackColor = true;
        this.btnSearch.Click += new System.EventHandler(this.btnSearch_Click);
        // 
        // lblVersion
        // 
        this.lblVersion.AutoSize = true;
        this.lblVersion.Location = new System.Drawing.Point(3, 485);
        this.lblVersion.Name = "lblVersion";
        this.lblVersion.Size = new System.Drawing.Size(64, 13);
        this.lblVersion.TabIndex = 4;
        this.lblVersion.Text = "Version: null";
        // 
        // linklblChangelog
        // 
        this.linklblChangelog.AutoSize = true;
        this.linklblChangelog.Location = new System.Drawing.Point(96, 485);
        this.linklblChangelog.Name = "linklblChangelog";
        this.linklblChangelog.Size = new System.Drawing.Size(58, 13);
        this.linklblChangelog.TabIndex = 5;
        this.linklblChangelog.TabStop = true;
        this.linklblChangelog.Text = "Changelog";
        // 
        // button1
        // 
        this.button1.Location = new System.Drawing.Point(640, 480);
        this.button1.Name = "button1";
        this.button1.Size = new System.Drawing.Size(75, 23);
        this.button1.TabIndex = 7;
        this.button1.Text = "test";
        this.button1.UseVisualStyleBackColor = true;
        this.button1.Visible = false;
        this.button1.Click += new System.EventHandler(this.button1_Click);
        // 
        // pictureBox1
        // 
        this.pictureBox1.Image = ((System.Drawing.Image)(resources.GetObject("pictureBox1.Image")));
        this.pictureBox1.Location = new System.Drawing.Point(232, 85);
        this.pictureBox1.Name = "pictureBox1";
        this.pictureBox1.Size = new System.Drawing.Size(247, 256);
        this.pictureBox1.TabIndex = 8;
        this.pictureBox1.TabStop = false;
        // 
        // Home
        // 
        this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
        this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
        this.BackColor = System.Drawing.SystemColors.GradientInactiveCaption;
        this.ClientSize = new System.Drawing.Size(727, 512);
        this.Controls.Add(this.pictureBox1);
        this.Controls.Add(this.button1);
        this.Controls.Add(this.linklblChangelog);
        this.Controls.Add(this.lblVersion);
        this.Controls.Add(this.btnSearch);
        this.Controls.Add(this.label2);
        this.Controls.Add(this.label1);
        this.Controls.Add(this.btnNewEntry);
        this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
        this.Name = "Home";
        this.StartPosition = System.Windows.Forms.FormStartPosition.Manual;
        this.Text = "Home";
        this.FormClosed += new System.Windows.Forms.FormClosedEventHandler(this.Home_FormClosed);
        ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).EndInit();
        this.ResumeLayout(false);
        this.PerformLayout();

    }

为什么它只在 XP 上在 InitializeComponent() 上崩溃?为什么它刚刚开始这样做?该领域最初几周的开发进展顺利。

我已经尝试取出整个 Update() 过程,但这并没有改变任何东西。

感谢阅读。

更新: Jon Skeet 建议我使用的图标使程序崩溃。事实证明确实如此。删除图标可以让表单在 XP 上正常工作(同样,在 7 上从来不是问题)。更有趣的是,这个问题等了 2 周后才显现出来,因为我在开发中使用这个图标已经很长时间了。稍后我会攻击它发生的原因,但与此同时我还有更多代码要编写。

谢谢大家。

【问题讨论】:

  • 从错误信息中,我猜你正在使用 XP 不知道如何渲染的图标资源。
  • 我同意花费者的观点。它肯定与您使用的在 InitializeComponent 函数中引用的两个图像之一有关。猜你可以快速缩小范围。
  • 您可以注释掉图标分配,看看它是否仍然崩溃。

标签: c# winforms


【解决方案1】:

鉴于堆栈跟踪,我怀疑这是问题所在:

this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));

我会通过删除图标来确认这一点,看看是否能解决问题。假设确实如此,请查看图标的格式 - 我想知道它是否以某种 XP 无法处理的方式损坏,或者它使用的图像格式比 XP 支持的更新。

当然,如果图标不重要,您可以完全忽略它 :) 不过,我会尝试诊断它发生了什么...

【讨论】:

  • 宾果游戏。删除表单上的该行允许应用程序的主 GUI 正常运行。但是,它仍然在使用原始图标的其他表单上崩溃。所以图标是罪魁祸首,但我仍然不确定为什么。同样,到目前为止,它在过去 2 周内一直在工作。右键单击属性显示它是一个被阻止的文件,因为它来自另一台计算机(在线 .ICO 转换器)。最初是 .PNG,现在是 .ICO。奇特的...图标现在将是我添加回来的最后一件事。我也想找出原因,但我快到最后期限了。感谢您的帮助!
【解决方案2】:

InitializeComponent() 方法中设置一个断点,然后逐步执行。

就像 Jon 已经说过的那样,您的图标可能有问题:

    this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));

调试器应该能够逐行执行,直到您遇到有问题的代码部分。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-03-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-28
    相关资源
    最近更新 更多