CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 16
  1. #1
    Join Date
    Aug 2005
    Posts
    57

    Stack overflow on function call

    Hello,

    I am getting a stack overflow in the moment when I call one of my own functions. By stepping with the debugger, I can see that the overflow takes place when the function body is entered.

    At first I thought I would perhaps overwrite memory somewhere, thus overwrite the function itself. But I kept monitoring the function address and it doesn't change from the moment of start up, so it is not overwritten.
    I of course do pass correct parameters types and values.

    What causes can there be for such a thing?

    Here is the disassembled code - the overflow occurs on the last line. (the normal code wouldn't help here since there is no code representation for "entering the body")
    Code:
    bool temp2=!Run(temp);
    00416FF5  movzx       eax,byte ptr [temp] 
    00416FFC  push        eax  
    00416FFD  call        Run (411005h) 
    00417002  add         esp,4

  2. #2
    Join Date
    Mar 2002
    Location
    St. Petersburg, Florida, USA
    Posts
    12,125

    Re: Stack overflow on function call

    Stack overflow is cause (usually) by one of 2 things:

    1)Excessive LOCAL variables
    2) Unlimited (or extremely deep) recursion.

    Post a minimal yet complete (every line has something to do with the problem, and the problem can be reproduced) piece of code and there will be plenty of people to help...
    TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
    2008, 2009,2010
    In theory, there is no difference between theory and practice; in practice there is.

    * Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
    * How NOT to post a question here
    * Of course you read this carefully before you posted
    * Need homework help? Read this first

  3. #3
    Join Date
    Aug 2005
    Posts
    57

    Re: Stack overflow on function call

    Well the problem is, the program is thousands of lines long, and at least half of the code is executed before the function that will crash.
    So what exactly can I post here that would help? (since the code before doesn't really have to do much with the function crashing (except for the memory overwrite somewhere), and the function crashing doesn't even enter its' body)

    As for extremely deep recursion or excessive local variables I don't think there is anything like that in the code - which I have written all myself.

    Let me ask another way: Can you give me any hint, how to find the line that overwrites some memory of my function body (the function header is not being overwritten as explained in my first post!) in this huge code with microsoft visual c++? I'm not very familiar with it yet.

    Thanks!

  4. #4
    Join Date
    Mar 2002
    Location
    St. Petersburg, Florida, USA
    Posts
    12,125

    Re: Stack overflow on function call

    Quote Originally Posted by deck42 View Post
    Let me ask another way: Can you give me any hint, how to find the line that overwrites some memory of my function body (the function header is not being overwritten as explained in my first post!) in this huge code with microsoft visual c++? I'm not very familiar with it yet.
    Set a breakpoint on the memory location that is being corrupted..

    In the more general case, there is a real issue in a development process, if you can end up with "thousands of lines" of code that you can not break down and test at a functional level to around 25-75 lines of code (neglecting "fall-through" initialization type code).

    The general "rule of thumb"s: A function/method implementation should fit on the screen without scrolling, A stateful class should have about a 3 to 1 ratio of methods to properties, A given class should contain under 25 total public items. This are, of course, just guidelines; but given 30 lines of code visible in the editor (average) * 8 methods, the largest class would have <250 lines of code.

    Aditionally the use of std::string, std::vector and the remainder of the STL containers and algorithms instead of "raw" arrays and pointers will dramatically reduce the chance of "overwrites".
    TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
    2008, 2009,2010
    In theory, there is no difference between theory and practice; in practice there is.

    * Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
    * How NOT to post a question here
    * Of course you read this carefully before you posted
    * Need homework help? Read this first

  5. #5
    Join Date
    Aug 2005
    Posts
    57

    Re: Stack overflow on function call

    Set a breakpoint on the memory location that is being corrupted..
    Well if I knew which one actually is being corrupted :-)
    I have tried to watch some addresses of my function, but they all stay unchanged.

    The problem is this can hardly be broken down to a few lines, because of the nature of the project: It needs and reads tons(!) of settings and then double checks them on every stage of execution. So if I remove the huge part of reading and digesting the settings, it will stop normal execution (it is meant to do that!). If I leave it in I already have at least a thousand lines of code before the function comes.

    Anyways, I have nailed down things a bit more precisely: It is actually not a direct memory overwrite (it seems), but when stepping along the disassembled code, it seems that execution enters an infinite loop. Just what I don't get is that this code is from "no source"(quote from disassembly) and it is executed right after entering the function body, but before reaching its' first line. So it's no loop inside the function...

    Any hints?

  6. #6
    Join Date
    Mar 2001
    Posts
    2,529

    Re: Stack overflow on function call

    And if you try to increase the reserved stack space does the same thing still happen?
    Last edited by ahoodin; November 16th, 2008 at 10:18 PM.
    ahoodin
    To keep the plot moving, that's why.

  7. #7
    Join Date
    Mar 2002
    Location
    St. Petersburg, Florida, USA
    Posts
    12,125

    Re: Stack overflow on function call

    Quote Originally Posted by deck42 View Post
    <snip>The problem is this can hardly be broken down to a few lines, because of the nature of the project: It needs and reads tons(!) of settings and then double checks them on every stage of execution. So if I remove the huge part of reading and digesting the settings, it will stop normal execution (it is meant to do that!). If I leave it in I already have at least a thousand lines of code before the function comes.
    </snip>
    Any hints?
    This is exactly what I am talking about. Without knowing the specifics of your program, I can make the following observations:

    1) There should be a class to process a single setting.
    2) There should be "unit tests" that verify this code against possible inputs to insure integrity and reliability
    3) There should be a class to "double check" (validate) each related combination of settings.
    4) There should be "unit tests" that verifies this code agains possible setting values.

    If you had the above 4 items, and they were comprehensively tested, then what would remain would simply be calls to these items. [remember my previous exclusion of fall-through initialization routines].

    IF the fallthrough sequence generated any problems, the list could easily be divided (replacing required items with hard-coded, and/or the "unit-test" could be updated to attempt to detect additional conditions.

    Unfortunately, if you already have a "large monolith", the choices are either just brute debugging (impossible to estimate how much time and/or if there will be ramining hidden problems) or "bite-the-bullet" and do a proper design.
    TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
    2008, 2009,2010
    In theory, there is no difference between theory and practice; in practice there is.

    * Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
    * How NOT to post a question here
    * Of course you read this carefully before you posted
    * Need homework help? Read this first

  8. #8
    Join Date
    Mar 2001
    Posts
    2,529

    Re: Stack overflow on function call

    Is it possible for you to post some code?



    Guessing has been fun!
    ahoodin
    To keep the plot moving, that's why.

  9. #9
    Join Date
    Feb 2000
    Location
    San Diego, CA
    Posts
    10,354

    Re: Stack overflow on function call

    What does the callstack show up in the debugger when it does crash with that exception? That is a very good starting point to look at all the functions the code has been through leading to the fault.

  10. #10
    Join Date
    Nov 2002
    Location
    California
    Posts
    4,556

    Re: Stack overflow on function call

    As suggested by Ahoodin, try re-building the program with an increased stack size, to see if the problem goes away. If it does, then you are probably doing one of the two things mentioned by TheCPUWizard, i.e. excessive recursion or excessive local variables. For example, did you write something like
    Code:
    Mystruct anArrayOfStruct[1000000];
    To re-build with an increased stack size, if using a Microsoft compiler, see "/F (Set Stack Size)" at http://msdn.microsoft.com/en-us/library/tdkhxaks.aspx

    Mike

  11. #11
    Join Date
    Mar 2013
    Posts
    2

    Re: Stack overflow on function call

    Although stack overflow exception seems to happen before function actually enters, it may be caused by function itself. Check if function that you try to enter does not allocate big-sized automatic variables. Hope it helps.

  12. #12
    Join Date
    Aug 2009
    Posts
    440

    Re: Stack overflow on function call

    This thread is over four years old. I'm sure the issue has been resolved.

  13. #13
    Join Date
    Mar 2013
    Posts
    2

    Re: Stack overflow on function call

    If that answer was here earlier today, it would help me, that means it may help others too.

  14. #14
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: Stack overflow on function call

    Quote Originally Posted by Rafal Moniuszko View Post
    If that answer was here earlier today, it would help me, that means it may help others too.
    It was.

  15. #15
    Join Date
    Apr 2015
    Posts
    1

    Re: Stack overflow on function call

    GCDEF you are wrong. It never was . Thank you very much Rafal Moniuszko for posting, despite the age of the thread. After googling for hours, your post was all I needed. Thanks men.

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured