Error Creating Window Handle

  • Thread starter Thread starter Joel Rumerman
  • Start date Start date
J

Joel Rumerman

I have an application that does the following in one of its user controls:

It creates a grid of panels (9x9) and then in each of those panels it places
a label (9x9) labels. There are other controls on the page and so I think
the total number of controls is 200. (81+81 + 35 or so.) Randomly, I'm
getting Error Creating Window Handle errors.

This user control gets built and rebuilt as the user navigates a the browser
control on the right. They may be looking at 15+ user controls in a span of
45 seconds.

Few Questions:
a. Could it be because I have too many controls on the page and they are
not getting destroyed properly?
b. If I open up Task Manger and add Handles to the Columns is this a
good indicator of the resources my application is using?
c. How else can I fix this problem.

The application uses all managed resources. It's running 1.1 and is
programmed in VB.NET. The machine is Windows XP Pro with 512Mb RAM.

Thx, Joel
 
So I played around with Task Manager adding Handles, User OBject, and GDI
Objects to the columns collection and lo-and-behold, when User Objects
reached 10000, BOOM! The program crashed. So, I looked into my code, wrote
some cleanup code, handled the destruction of objects in the dispose method
myself and wham ... User Objects never gets above 175, and it no longer
crashes.

Stupid me, I was re-creating the user control, but not disposing of the
previous labels and panels.

So, for all you folks who have had trouble with Window Handles, it may not
be the window handles, but the User Objects counter that's important. If you
keep creating objects on a form without destroying the previous one due to
some input or from the user or something else... you'll probably having the
same problem.

-Joel
 
Hi Joel,

I am very glad your problem resolved :-).

In .Net, there are 2 types of resource freeing: managed objects and
unmanaged resources. All .Net class instances are managed resource, CLR will
add reference counters on these objects, once one instance's reference count
drops to 0, it will be implicitly collected by GC. For unmanaged resources,
they are not .Net's type, but the resource of Windows, such as windows
handle, database connections, bitmap memory, etc. .Net will not manage
resource counter for them, .Net use IntPtr pointer to marshal them, .Net has
nothing knowledge of them and we should handle the resource free of them
ourselves. Implicitly, GC will invoke finalize method before collect the
managed resource, finalize method will free the unmanged sources. But when
unmanaged resource is rarity, such as the memory is not large for bitmap, or
our application uses too much Win32 resouces, there should be a way to
explicitly free the unmanaged resource, this explicit way is Dispose method.

A USER object is an object from Window Manager, which includes windows,
menus, cursors, icons, hooks, accelerators, monitors, keyboard layouts, and
other internal objects. They are unmanaged resources. We should override the
Dispose method of our customized control or class and freeing all the
unmanaged resources in this method.

Hope this makes sense to you. Have a good weekend!

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
Back
Top