If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register or Login
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

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")

Re: Stack overflow on function call

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,2010In 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

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.

Re: Stack overflow on function call

Originally Posted by deck42

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,2010In 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

Re: Stack overflow on function call

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...

Re: Stack overflow on function call

Originally Posted by deck42

<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,2010In 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

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

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.