[.net] Window Handle Leaks (dispose problem I think)

Started by
2 comments, last by gellin 17 years, 10 months ago
Alright I have a program that contains a form call Room. This Room form contains about 400 picture boxes. The problem I have is opening all of these Rooms into tabs. Once I open so many tabs I run out of window handles and it crashes. If I try and dispose of the room once I open up a new tab, when I go back to it I lose all of the pictureboxes that were in that room tab. Do I have no choice but to dispose of them every time I go to a new tab and when I want to go back to that tab I have to load them all up again?
Advertisement
I think your question is wrong. A good question would be "Do I really need that many picture boxes?". And the answer is: "no".
--AnkhSVN - A Visual Studio .NET Addin for the Subversion version control system.[Project site] [IRC channel] [Blog]
Your program is far too aggressive on Windows resources (handles and such), especially for what it does (as I understand it: display a collection of pictures).
400 picture boxes times, say 10 tabs, that's 4'000 boxes... Imagine if every single application used as many resources!

I suggest 2 different solutions:

1/ If your layout is simple and there's no complex user interactions: consider writing your own "picture list" control. Override OnPaint and use GDI+ to render it. This way you use only one handle, instead of 400.

2/ If you think that you benefit a lot from the windows infrastructure, and you don't want to use the first solution, you can use virtualization. At two different levels:

2a/ UI Virtualization. Most probably, your 400 pictures are not visible at the same time. I suppose there's some scrolling, and that something like 40 pictures are visible at the same time. The trick is to instantiate just a little more picture boxes (e.g. 50), and re-use those that leave the screen to fill the screen on the opposite side (i.e. you move them and change the picture in it). This way, you use a constant number of handles (which is just slightly more than what is visible on screen), no matter how big your list is.

2b/ Also consider Data Virtualization. It's probably not reasonable to have 4'000 different pictures loaded in memory at the same time (especially if they are big). You can reduce your resources and memory footprint by dynamically loading them, when they are needed.

Hope it helps,
jods

PS: Just guessing: if this is part of some kind of tile engine, you should definitively go with solution 1.
No I don't really need that many picture boxes and I could do the GDI stuff but it was by far easier for this application (a level editor) if it was done using the picture boxes.

I figured out how I can dispose of those picture boxes when I close a room tab and just recreate them right after that. Since they are not displayed yet, they don't take up and handles.

I thought of disposing them every time you click on a new tab and then reloading it all back when you go back to the tab but it was a little bit too slow for my tastes. Instead I just limit the number of tabs that you can have open at one time to 10.

Thanks guys.

This topic is closed to new replies.

Advertisement