The Design of Software (CLOSED)

A public forum for discussing the design of software, from the user interface to the code architecture. Now closed.

The "Design of Software" discussion group has been merged with the main Joel on Software discussion group.

The archives will remain online indefinitely.

stack overflow in C++

What are some of the symtoms of a stack overflow in C++. I have a GUI app that starts to funk up after I add a lot of stuff on the stack...does a program usually just crash or does the OS try to compensate by deleting memory elsewhere?

Sunday, May 25, 2008
The symptom is you get a "stack overflow" exception... usually it's an infinite loop in your code.
Sunday, May 25, 2008
Im try to figure out why my program runs fine for a while, but then all of the sudden the GUI goes out and wont draw bitmaps or anything else

Sunday, May 25, 2008
It sounds like you aren't releasing Windows resources, like bitmaps, pens, brushes etc.
Neville Franks Send private email
Monday, May 26, 2008
Since your question was not submitted in audio form I'm afraid we cannot answer.

Monday, May 26, 2008
So your question is basically: "Why does my program not work?  To provide some details about the problem I'm encountering: my program does something with graphics."?

I'm undecided whether to answer "you probably have some sort of bug in your program" or suggesting you should use C# and do the graphics in GTK#...
a2800276 Send private email
Monday, May 26, 2008
By default VC++ allocates 1Mega bytes to stack. If you have a very large object or a deep recursion function, stack overflow will happen.

You can increase the stack size with /STACK option of link.exe. Or you might want to rewrite your code to reduce the need for bigger stack size.
Monday, May 26, 2008
How do you pull pen, bitmap and brush resources off the stack in MFC ?

Monday, May 26, 2008
It's not a stack overflow - that would show up as a crash.

Like Neville mentioned, you're most likely leaking GUI resources such as brushes, bitmaps, pens, etc... - there are only so many of these that can be created and if you keep creating them without cleaning up previously allocated ones, eventually you won't be able to create more.

That's what usually fits the symptoms that you are describing.
Monday, May 26, 2008
Thanks michael, how do you clean up these resources? Im using MFC.

Tuesday, May 27, 2008
Typically to switch to a new brush colour you have to store the current one and restore it.
    CBrush      *pOldBrush;   
    pOldBrush = dc.SelectObject(&m_rush);
    ....draw stuff....

You do get the feeling sometimes that MFC wasn't designed by the greatest minds in the world, or even in Microsoft.
Martin Send private email
Tuesday, May 27, 2008
Yeah, you're probably not doing the part that Martin says - if the object is still selected in the device context when you try to delete it, I think your delete may be ignored.

Look here for some details:

Make sure you follow those 4 steps as documented (especially note step 3):

To create a graphic object in a device context

  1. Define a graphic object on the stack frame. Initialize the object with the type-specific create function, such as CreatePen. Alternatively, initialize the object in the constructor. See the discussion of one-stage and two-stage creation, which provides example code.
  2. Select the object into the current device context, saving the old graphic object that was selected before.
  3. When done with the current graphic object, select the old graphic object back into the device context to restore its state.
  4. Allow the frame-allocated graphic object to be deleted automatically when the scope is exited.
Tuesday, May 27, 2008
Also, for the reference: the limit of GDI handles in XP is 10,000 per process.
asmguru62 Send private email
Tuesday, May 27, 2008
Further, you can add a column to Task Manager that shows GDI handles in use in each app.  Watch it while your app runs and see if it goes up.
Tuesday, May 27, 2008
Doug, asm, & Michael. ...Thank helped me solve my problem using the GDI monitor in the task manager!

Tuesday, May 27, 2008
"Yeah, you're probably not doing the part that Martin says - if the object is still selected in the device context when you try to delete it, I think your delete may be ignored."

Like, just ignored? Funny.

This was designed before they had exceptions, wasn't it?
quant dev Send private email
Thursday, May 29, 2008

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics
Powered by FogBugz