Stack Overflow error

 

Hey, I have an EA that gets the Stack Overflow error if I don't put a Sleep of something like 1 second. I do have a loop that calls itself (something like an infinite loop) but I need to find a way to do this and not get the Stack Overflow error.

The infine loop is something like this:

My EA searches for a good stock, if it doesn't find any, start all over again (calling the same function where this code is inside).

Ex:

void Search()

{

if(IsGood)

{

//Do anything

}else{

Search();

}

}


Thanks

 
LucasHeise:

Hey, I have an EA that gets the Stack Overflow error if I don't put a Sleep of something like 1 second. I do have a loop that calls itself (something like an infinite loop) but I need to find a way to do this and not get the Stack Overflow error.

The infine loop is something like this:

Based upon what you've written, I would suggest that you pull the loop logic out of the search logic.

Have your Search() function return a boolean:

  true = found; false = not found.

Then, have a loop that calls your Search() function.

You might even rename your function to something logical to enhance readability, e.g.

bool IsGoodStock()
{
    // returns true if good stock found; false otherwise
}

Then you might do something like:

while ( true )
{
    if ( IsGoodStock() )
    {
        // do something
    }
}

So, you get the benefit of the infinite loop without loading up the stack.

You'll probably want some logic to get you out of the infinite loop.

 
Anthony Garot:

Based upon what you've written, I would suggest that you pull the loop logic out of the search logic.

Have your Search() function return a boolean:

  true = found; false = not found.

Then, have a loop that calls your Search() function.

You might even rename your function to something logical to enhance readability, e.g.

Then you might do something like:

So, you get the benefit of the infinite loop without loading up the stack.

You'll probably want some logic to get you out of the infinite loop.

Thanks, I got it working this way...

Thanks again

 
LucasHeise: Stack Overflow error if I don't put a Sleep of something like 1 second.

void Search(){

  if(IsGood){

     //Do anything

  }else{
     Search();
  }
}
  1. When you post code please use the CODE button (Alt-S)! (For large amounts of code, attach it.) Please edit your post.
              General rules and best pratices of the Forum. - General - MQL5 programming forum
              Messages Editor

  2. Sleep is irrelevant. You have recursive loop. Search is calling Search. Search should loop through your items and find what you want.
 
A StackOverflowError is simply signals that there is no more memory available. It is to the stack what an OutOfMemoryError is to the heap: it simply signals that there is no more memory available. JVM has a given memory allocated for each stack of each thread, and if an attempt to call a method happens to fill this memory, JVM throws an error. Just like it would do if you were trying to write at index N of an array of length N. No memory corruption can happen. The stack can not write into the heap.

The common cause for a stackoverflow is a bad recursive call. Typically, this is caused when your recursive functions doesn't have the correct termination condition, so it ends up calling itself forever. Or when the termination condition is fine, it can be caused by requiring too many recursive calls before fulfilling it.

Here's an example:

public class Overflow {
public static final void main(String[] args) {
main(args);
}
}

That function calls itself repeatedly with no termination condition. Consequently, the stack fills up because each call has to push a return address on the stack, but the return addresses are never popped off the stack because the function never returns, it just keeps calling itself. If the stack is full you can't push, if you do you'll get stack overflow error. StackOverflowError is avoidable if recursive calls are bounded to prevent the aggregate total of incomplete in-memory calls (in bytes) from exceeding the stack size (in bytes).


What actually causes a StackOverflow error in Java?
What actually causes a StackOverflow error in Java?
  • net-informations.com
A StackOverflowError is simply signals that there is no more memory available. It extends the VirtualMachineError class, which indicates that the JVM (Java Virtual Machine) is broken, or it has run out of resources and cannot operate. If you have a function like: In the above code myFunction() will keep calling itself, getting deeper and...
Reason: