tags:

views:

129

answers:

1

i have a probably stack overflow in my application (off course, only in release mode...), and would like to add some protection/investigation code to it. i am looking for a windows API to tell me the current state of a thread stack (i..e, the total size and used size). anyone ?

thx Noam

A: 

The total size of the stack will be the size of the stack you asked for when you created the thread ( or linked the program if it's the main thread ).

There are some preliminary references to getting the stack size for a thread pool in Windows 7 on MSDN ( QueryThreadpoolStackInformation ).

As an approximation, you can compare the address of a local variable with the address of another local variable further down the stack to get a measure of the amount us. I believe that how a program running in windows chooses to lay its local variables out in the virtual memory space windows allocates to a thread is up to the implementation of that language's runtime, rather than something that Windows really knows about; instead you get an exception when you attempt to access an address just below the memory allocated for the stack.

The other alternative to complicating your code with a check whether the stack has reached a limit is to add an exception handler for EXCEPTION_STACK_OVERFLOW, which will get called by the OS when it checks that the stack has reached its limit. There's an example here.

Pete Kirkham