Re: What cause " stack overflow " ?
Li Hang wrote:
After I revised the program, the problem don't show again. But I
still don't know the reason.
Well, retrace your steps until you find the one that caused the failure.
In my program, It is not because of recursive calls or too deep
nested scope or too many auto vairables.
I forgot to mention: it could also be few but too big auto variables, just
in case that wasn't clear.
I use CTrackPanel in a dialog, and the dialog object is dynamicly
created. if I forgot to release the GDI object such as CBrush, or
forgot to release the heap memory would cause "stack overflow" or
" access memory violation"?
Generally, leaking GDI objects or memory doesn't cause stack overflows or
access memory violations. However, it might cause new/malloc to fail and if
you are not prepared for the failure[1] you might end up with a null
pointer and get an access violation when you access that.
Another cause for stack overflow is a random stack corruption btw. Newer
versions of MSC have some stack protection mechanisms available and help
detecting such errors. This is typically the case when the behaviour is not
consistent, real stack overflows can typically be easily reproduced by
feeding the program the same input again.
It is also possible that your program simply needs a bigger stack because it
needs lots (but a fixed, limited number) of stack memory. In that case, you
can adjust the stacksize either in the linker settings or in the parameters
when creating a thread.
By the way, how to log the stack value?
Watching the stack isn't that easy. There is a (platform-dependant) way to
protect against stack overflows which is used by Boost's parser for regular
expressions. You could take a look at that code for example.
good luck
Uli
[1] Older versions of MS C++ implementations make it very easy to not
detect 'new' failures by not even throwing bad_alloc on failure. New
computers with lots of RAM and swapspace make it very unlikely that
allocations with malloc() fail. Both add up to letting sloppy
programming slip by for a long time before 'strange' behaviour suddenly
and unexpectedly comes up.