Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

CreateWindow limit?

qhimqqhimq Member Posts: 16
Hi,

Is there a limit to how many windows an application can create?

Because my program creates a few hundred child windows...and then doesn't create anymore.

Thanks.

Comments

  • AsmGuru62AsmGuru62 Member Posts: 6,519
    : Hi,
    :
    : Is there a limit to how many windows an application can create?
    :
    : Because my program creates a few hundred child windows...and then
    : doesn't create anymore.
    :
    : Thanks.
    :
    [link=http://support.microsoft.com/kb/327699]http://support.microsoft.com/kb/327699[/link]

    [color=Blue]It talks about 10,000 handles, but you may have been allocating additional handles for each child HWND you created.

    MSDN mentions that it is a poor application design if application must have that many handles.[/color]
  • qhimqqhimq Member Posts: 16
    I maybe able to make over 10,000 windows, however my program destroys almost each window it creates right away.

    I put about 30 or so createWindow calls in my WM_PAINT, then destroy them after they show up. Since the parent of all those windows created in the paint message are resizeable and movable, the paint message gets called a LOT.

    No matter what though, I'm force to program this function in a more efficient way because eventually the CreateWindow function fails.
  • AsmGuru62AsmGuru62 Member Posts: 6,519
    : I maybe able to make over 10,000 windows, however my program
    : destroys almost each window it creates right away.
    :
    : I put about 30 or so createWindow calls in my WM_PAINT, then destroy
    : them after they show up. Since the parent of all those windows
    : created in the paint message are resizeable and movable, the paint
    : message gets called a LOT.
    :
    : No matter what though, I'm force to program this function in a more
    : efficient way because eventually the CreateWindow function fails.
    :
    [color=Blue]
    You should be aware that in response to WM_PAINT you can only PAINT and not create windows.

    If you tell me the nature of your application - maybe, we can find better design. As MSDN correctly mentions: that many windows is a poor design.

    Sometimes you need to have a lot of objects in your program hosted by a window, so instead of creating an HWND for each of these objects simply keep a rectangle coordinates of a client area of a host window occupied by an object and then go from there. If you need to detect a click on the object, then when click comes to host window - check its coordinates against that array of rectangles. No need for huge array of HWNDs.
    [/color]
  • qhimqqhimq Member Posts: 16
    For some reason I was able to figure out a new solution. However the new solution has a new problem.

    https://netfiles.uiuc.edu/qmitche2/www/DCP.exe

    Create several files (about 10) then go to File -> Setup Print Layout
    Add all the charts you just created.
    Resize, move them all, and try to overlap different random charts over one another. Each chart is really a child window.

    Takes about 3 minutes of random flirting, but something very bad happens.
    First: The text does not resize. Its using the DrawTextEx() function.
    Second: Everything on the screen disappears except for the client area of the print layout window. Thats including all title bars, taskbar, etc.

    I added the GetLastError function in the WM_PAINT which is where the drawtext function is, and no error message printed out at all. (I used printf to print it out on the console window)

    My new solution is the exactly the same as what I did before, however I put all the code I had creating/destroying windows in my AddChart function rather than my WM_PAINT message. Once I created all those windows I destroyed them and kept all the info I wanted from them in a 4 dimensation char array.

    Then my WM_PAINT got all it's info from the huge array rather than creating windows. Until everything goes haywire it runs much much more smoother, faster, and uses less cpu than before.

    But it seems my new solution is still creating too many windows for the operating system to handle.

  • AsmGuru62AsmGuru62 Member Posts: 6,519
    : For some reason I was able to figure out a new solution. However
    : the new solution has a new problem.
    :
    : https://netfiles.uiuc.edu/qmitche2/www/DCP.exe
    :
    : Create several files (about 10) then go to File -> Setup Print Layout
    : Add all the charts you just created.
    : Resize, move them all, and try to overlap different random charts
    : over one another. Each chart is really a child window.
    :
    : Takes about 3 minutes of random flirting, but something very bad
    : happens.
    : First: The text does not resize. Its using the DrawTextEx()
    : function.
    : Second: Everything on the screen disappears except for the client
    : area of the print layout window. Thats including all title bars,
    : taskbar, etc.
    :
    : I added the GetLastError function in the WM_PAINT which is where the
    : drawtext function is, and no error message printed out at all. (I
    : used printf to print it out on the console window)
    :
    : My new solution is the exactly the same as what I did before,
    : however I put all the code I had creating/destroying windows in my
    : AddChart function rather than my WM_PAINT message. Once I created
    : all those windows I destroyed them and kept all the info I wanted
    : from them in a 4 dimensation char array.
    :
    : Then my WM_PAINT got all it's info from the huge array rather than
    : creating windows. Until everything goes haywire it runs much much
    : more smoother, faster, and uses less cpu than before.
    :
    : But it seems my new solution is still creating too many windows for
    : the operating system to handle.
    :
    :
    [color=Blue]
    I suspect, that you may have GDI leaks in your WM_PAINT code. Usually, the effect you describe (stopping functioning properly after a lot of drawing operations) is a sign of GDI leaks.

    Make sure you delete every pen and brush and font after you finished drawing. Also, if you create DC to paint in memory - delete it, when finished.
    [/color]
  • qhimqqhimq Member Posts: 16
    Thanks you were right, I only forgot to delete 1 font, and what a mess it created...

    Thanks again.

    btw, Its really weird how I had to include to just use DeleteFont(). You'd think they would have it in the same header as CreateFont().
  • qhimqqhimq Member Posts: 16
    Lets say I want to create acouple hundred Edit control windows. Do you think with 400 edit windows each with identifiers, that CreateWindow() would fail?
  • zibadianzibadian Member Posts: 6,349
    : Lets say I want to create acouple hundred Edit control windows. Do
    : you think with 400 edit windows each with identifiers, that
    : CreateWindow() would fail?
    :
    Dumping about 2000 handles onto the system is a very very bad idea. Not to mention the burden you put on your users to handle that many windows.
    A better alternative to having 400 windows with 400 edits, is one stringgrid with 400 rows and 2 columns, one for the label and one for the value. This is a much lighter load for the system and much easier to manage for the user, while still having more or less the same functionality.
  • AsmGuru62AsmGuru62 Member Posts: 6,519
    : Thanks you were right, I only forgot to delete 1 font, and what a
    : mess it created...
    :
    : Thanks again.
    :
    : btw, Its really weird how I had to include to just use
    : DeleteFont(). You'd think they would have it in the same header as
    : CreateFont().

    [color=Blue]Instead of DeleteFont() use DeleteObject(hFont) - then you do not need the

    Also, for the future: it is very easy to detect if your code has GDI leaks.

    1. Launch Task Manager (right mouse click on a task bar and select "Task Manager")

    2. Select the "Processes" tab
    3. Select "View" -> "Select Columns" item from a menu bar
    4. Find and select your application from a list of processes
    5. Resize your application a few times and watch the GDI count in one of the columns - it should stay the same value. Open some dialog and see if count increased - if it did - it should go back to the same value when dialog is closed. If every time dialog opens it adds to the count, then the dialog has a GDI leak(s).

    The leaks can appear even when you just move mouse over some areas.[/color]
Sign In or Register to comment.