Skip to main content

 none
How to handle "Stack Overflow"? RRS feed

  • Question

  • Hi All,

    I am currently developing a user mode service using vc++, while testing I have seen my service is getting crashed due to the "Stack Overflow" error.

    I have created a VM with 1 Gb RAM and 1 core, for this machine I have disabled the paging then also with I am running "TestLimit.exe -d" which will leak the memory as much as possible.

    Then my service crashes randomly with "Stack Overflow" because there is no memory to allocate for thread stack.

    So how should I handle this case?

    Thanks


    Thanks SOU

    Sunday, March 25, 2018 3:11 AM

All replies

  • Hi All,

    I am currently developing a user mode service using vc++, while testing I have seen my service is getting crashed due to the "Stack Overflow" error.

    I have created a VM with 1 Gb RAM and 1 core, for this machine I have disabled the paging then also with I am running "TestLimit.exe -d" which will leak the memory as much as possible.

    Then my service crashes randomly with "Stack Overflow" because there is no memory to allocate for thread stack.

    So how should I handle this case?


    I think you need to clarify exactly what you are experiencing.

    For example, exactly what errors (text and codes) are received when the "crash" happens?

    Does the thread in question actually overflow its stack or does the call to create the thread fail due to the low memory conditions?

    Are you using the default stack size when you create the thread?

    How does your code use the stack?  Recursive functions?  Allocation of large amounts of data?

    Finally, considering the rather extreme test conditions (1 GB VM (unknown windows version), no paging file and maximum memory leaks) it seems that the intent is to test for code that fails to gracefully handle errors in low memory conditions.  It looks like you may have found some.



    • Edited by RLWA32 Sunday, March 25, 2018 12:17 PM clarified comment
    Sunday, March 25, 2018 11:27 AM
  • After crashing I am getting "Unhandled Exception Detected - Reason: Stack Overflow (0xc00000fd)"

    The thread which I found in dump file it is actually custom error logging thread, 

    I am pretty much sure that this thread is not doing recursion also stack size is the default (1MB).

    As per windbg, a thread is already used 40KB and now it is on the guarded page and while making attempt to commit new one page it crashes due to low memory.

    Also, this situation is not easily reproducible.

    Note - I have also seen one crash which is also happened at another location in code (other than logging thread).

    What I understand is, as a developer there are not many things on hand to fix this "Stack overflow" in low memory situations if there is no recursion in the thread.

    Thanks


    Thanks SOU


    Sunday, March 25, 2018 12:16 PM
  • I think it is unlikely that most Windows systems today run with 1GB of memory and no paging file.

    Is there a reason for using such extreme test conditions? 

    You could try to use SEH to catch the exception, but there is no point in continuing considering the nature of the exception.
    • Edited by RLWA32 Sunday, March 25, 2018 12:27 PM
    Sunday, March 25, 2018 12:24 PM
  • 40 KB is a reasonable stack size for one thread. Especially when there's a lot of threads. Find if this thread indeed has been created with this stack size. Either increase the size, or stop storing large things on the stack.

    -- pa


    • Edited by Pavel A Sunday, March 25, 2018 7:48 PM
    Sunday, March 25, 2018 7:48 PM
  • Thinking some more about it, a stack overflow exception attributable to low memory such that the system cannot extend a stack when necessary is a known possible outcome of the circumstances.
    Sunday, March 25, 2018 10:41 PM
  • Yup, I have verified that thread was created with default stack size and that I have verified in dump file as well.

    Thanks SOU

    Monday, March 26, 2018 5:31 AM
  • True, I also believe so, in such scenarios thread will be in trouble at any point.

    For example,

    1. The thread is about to call a function but before that he needs to push parameters to the stack and for that, it tries to commit the page but if there is no memory then for sure, there will be a stack overflow.

    2. Also while creating local variables on the stack, a thread might commit a page, and if there is no memory then outcome will be the same.

     

    Thanks SOU

    Monday, March 26, 2018 5:43 AM