Error Creating Window Handle

scottdg

New member
Joined
Jul 10, 2018
Messages
4
Programming Experience
3-5
Let me start by saying I am not an experienced developer and by no means a C# expert.


I have created a winform application though that I am having a problem with. It is a case management program that the user uses to search for cases. When selecting a case it opens in the current panel. The user can modify the case and save it. I was not disposing of any of the controls and the user objects would increase to about 1,100 when it eventually failed with the "Error Creating Window Handle". I have since disposed of the current panel on the button click event and have cut down considerably on the number of user objects but I am still getting the error after 10 searches and subsequent saves of the searched for case.


I am calling the following method to open the form to modify the case and to return to the search page when saved.


1628170824200-gif.1634

C#:
 public void nav(Form form, Panel panel)
        {
            form.TopLevel = false;
            form.Size = panel.Size;
            panel.Controls.Clear();
            panel.Controls.Add(form);
            form.Show();
        }
 
I have created a winform application
That was the first mistake. You should have created a WPF or UWP application. Microsoft originally was going to abandon WinForms. They were not going to invest anymore resources into it and just let it die into obsolescence as .NET Framework 4.8 would eventually reach end of life. It was only due to the pressure of so many legacy WinForms applications that had already been deployed as in-house line-of-business applications that MS put it back into the .NET Core 3.1 and higher.

I would highly recommend moving away for WinForms.

Now as for your current problem. You really need to get to the bottom of the GDI object leak. Taking a shotgun approach of randomly trying to force disposal of objects is not really productive. You may or may not get lucky and figure out which thing needs to be disposed. It would be much better if you took the time to analyze your code and find out where all the objects are being leaked from.

The following is an old article that focuses mostly on MFC and Win32 API programming, but it contains great information. In case you didn't know, WinForms is just a thin wrapper around the Win32 APIs, just like MFC is a wrapper around the Win32 API.

 
That was the first mistake. You should have created a WPF or UWP application. Microsoft originally was going to abandon WinForms. They were not going to invest anymore resources into it and just let it die into obsolescence as .NET Framework 4.8 would eventually reach end of life. It was only due to the pressure of so many legacy WinForms applications that had already been deployed as in-house line-of-business applications that MS put it back into the .NET Core 3.1 and higher.

I would highly recommend moving away for WinForms.

Now as for your current problem. You really need to get to the bottom of the GDI object leak. Taking a shotgun approach of randomly trying to force disposal of objects is not really productive. You may or may not get lucky and figure out which thing needs to be disposed. It would be much better if you took the time to analyze your code and find out where all the objects are being leaked from.

The following is an old article that focuses mostly on MFC and Win32 API programming, but it contains great information. In case you didn't know, WinForms is just a thin wrapper around the Win32 APIs, just like MFC is a wrapper around the Win32 API.

Yeah - I wish I had done it in WPF but I had done something else in WinForms so I went with that for now. I plan on redeveloping it in WPF eventually.
Thanks, I will take a look at the article. When I get the error there are around 350 GDI objects. From what I see of other applications running and what I have read that doesn't seem like a lot.
 
Yeah, 350 would have been relatively high in the Win 3.x days, but that should have been about average in the Win 9x days. What to the .NET Framework performance counters say?
 
Back
Top Bottom