Skip to content

Access icon failed with exception "Cannot access a disposed object" #13963

@zhenhuh

Description

@zhenhuh

.NET version

.NET10 RC1 and RC2

Did it work in .NET Framework?

Yes

Did it work in any of the earlier releases of .NET Core or .NET 5+?

yes, works in .NET 10 preview 7

Issue description

I set the form.Icon to an existing one, and then close this form, but when I try to use the icon again, I get an error "Cannot access a disposed object."
But I found that not all the icons cause this problem.

Steps to reproduce

Here is the demo code,

    public partial class Form1 : Form
    {
        private Icon ico;

        public Form1()
        {
            InitializeComponent();
            ico = new Icon("D:\\TestWinformIcon\\exception.size32.ico");  // change the path on your machine
        }

        private void button1_Click(object sender, EventArgs e)
        {
            var form = new Form2();
            form.Icon = ico;
            form.Show();
            form.Close();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            MessageBox.Show(ico.Handle.ToString());
        }
    }

First click button1 and then click button2, exception dialog pops.

Image

Full code here with icon included:

TestWinformIcon.zip

Metadata

Metadata

Assignees

Type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions