+1  A: 

Q1

Sounds like you're trying to create far too many UI controls at the same time. Even if there's memory left, you're running out of handles. See below for a brief, but fairly technical explanation.

Q4

I understand a user object to be any object that is part of the GUI. At least until Windows XP, the Windows UI API resided in USER.DLL (one of the core DLLs making up Windows). Basically, the UI is made up of "windows". All controls, such as buttons, textboxes, checkboxes, are internally the same thing, namely "windows". To create them, you'd call the Win32 API function CreateWindow. That function would then return a handle to the created "window" (UI element, or "user object").

So I assume that a user object handle is a handle as returned by this function. (Winforms is based on the old Win32 API and would therefore use the CreateWindow function.)

Q2

Indeed you cannot create as many UI controls as you want. All those handles retrieved through CreateWindow must at some point be freed. In Winforms, the easiest and safest way to do this is through the use of the using block or by calling Dispose:

using (MyForm form = new MyForm())
{
    if (form.ShowDialog() == DialogResult.OK) ...
}    

Basically, all System.Windows.Forms.Control can be Disposed, and should be disposed. Sometimes, that's done for you automatically, but you shouldn't rely on it. Always Dispose your UI controls when you no longer need them.

Note on Dispose for modal & modeless forms:

  • Modal forms (shown with ShowDialog) are not automatically disposed. You have to do that yourself, as demonstrated in the code example above.
  • Modeless forms (shown with Show) are automatically disposed for you, since you have no control over when it will be closed by the user. No need to explicitly call Dispose!

Q5

Everytime you create a UI object, Winforms internally makes calls to CreateWindow. That's how handles are allocated. And they're not freed until a corresponding call to DestroyWindow is made. In Winforms, that call is triggered through the Dispose method of any System.Windows.Forms.Control. (Note: While I'm farily certain about this, I'm actually guessing a little. I may not be 100% correct. Having a look at Winforms internals using Reflector would reveal the truth.)

Q3

Assuming that your StrategyEditor creates a massive bunch of UI controls, I don't think you can do a lot. If you can't simplify that control (with respect to the number of child controls it creates), then it seems you're stuck in the situation where you are. You simply can't create infinitely many UI controls.

You could, however, keep track of how many StrategyEditors are opened at any one time (increase a counter whenever one is instantiated, and decrease it whenever one is closed -- you can track the latter using the FormClosing/FormClosed event of a form, or in the Dispose method of a control). Then you could limit the number of simultaneously opened StrategyEditors to a fixed number, say 5. If the limit is exceeded, you could throw an exception in the constructor, so that no more instances are created. Of course I can't say whether StrategyForm is going to handle an exception from your StrategyEditor constructor well...

public class StrategyEditor : ...
{
    public StrategyEditor()
    {
        InitializeComponent();

        if (numberOfLiveInstances >= maximumAllowedLiveInstances)
            throw ...;
        // not a nice solution IMHO, but if you've no other choice...
    }
}

In either case, limiting the number of instantiated StrategyEditors seems like a temporary fix to me and won't solve the real problem.

stakx
Thanks man! You solved all the easy problems! =D +1 for helping me out with good info. But I still need to know answer of Q3, if it is possible. Regards!
Nayan
Also, see the update above on why I cannot dispose windows whenever I want.
Nayan
OK, I added a note on the `Dispose` problem on modeless forms (under Q2) and an attempt at Q3.
stakx
Thanks again for the Modal and Modeless forms info. Never knew that! About Q3, we do track the usage of UOH via win32 APIs instead of just keeping a counter of children windows. But this is what I suspected - it would be tough to control the allocation action.
Nayan
I'll keep this question open for 2-3 days before I mark your answers as solution. Let's see if anybody can come up with better ideas :)
Nayan
Thanks man for editing again and again! :) Beauty is important, be it code. =D
Nayan
Hmm... I thought you said you know how to fetch the number of UOHs. I don't, to be honest. -- And I've really *never* come across a GUI with so many controls that the handle limit would be exceeded. How are your users going to cope with all those hundreds and thousands of UI controls? :-D -- I guess this is as far as I can help you. I'm also curious what others have to say about this.
stakx
Crazy customers!! They are stress testing the app and want it to live through catastrophe! Actually, the app eventually crashes and user loses valuable data. We are trying to teach him how to use a computer these days. Crazy ass operator.
Nayan
Its not just one form with hundreds of controls, but many forms with about 10 controls or so. Result is same. The user thinks he can open up 40 children windows and the app should run just fine! I'm wondering if there is any application, free or commercial, which can open up 50-60 children forms (moderately decorated with window handles)?
Nayan