-4

I keep getting a stack overflow error but I can't figure out why or how to fix it. Here is my code:

 private void ShowDiff(int leftIndex, int rightIndex)
        {
            if (leftIndex > 0 && rightIndex > 0 &&
                _compareFunc(_left[_preSkip + leftIndex - 1], _right[_preSkip + rightIndex - 1]))
            {
                ShowDiff(leftIndex - 1, rightIndex - 1);
                FireLineUpdate(DiffType.None, _preSkip + leftIndex - 1, -1);
            }
            else
            {
                if (rightIndex > 0 &&
                    (leftIndex == 0 ||
                     _matrix[leftIndex, rightIndex - 1] >= _matrix[leftIndex - 1, rightIndex]))
                {
                    ShowDiff(leftIndex, rightIndex - 1);
                    FireLineUpdate(DiffType.Inserted, -1, _preSkip + rightIndex - 1);
                }
                else if (leftIndex > 0 &&
                         (rightIndex == 0 ||
                          _matrix[leftIndex, rightIndex - 1] < _matrix[leftIndex - 1, rightIndex]))
                {
                    ShowDiff(leftIndex - 1, rightIndex);
                    FireLineUpdate(DiffType.Deleted, _preSkip + leftIndex - 1, -1);
                }
            }

        }

This is the error I keep getting:

An unhandled exception of type 'System.StackOverflowException' occurred in Comparer.exe

Any help is appreciated.

11
  • 2
    You need to carefully evaluate how the recursiveness of this method behaves, such that you will always end. Could it be that you're giving it a large piece of data so that even if it works exactly as it should, it's going to consume more than the default stack size which is 1MB? Commented Jan 29, 2015 at 14:03
  • 4
    Did you try stepping through it, or using debug prints to see what is happening? Commented Jan 29, 2015 at 14:03
  • It is a classical infinite loop. As suggested by @pquest, print the values before your compare function and where you call your recursive point (1st, 2nd or 3rd call). You will figure out easily where your loop is being infinite. Commented Jan 29, 2015 at 14:05
  • Another question that will help you debug your issue stackoverflow.com/questions/206820/… Commented Jan 29, 2015 at 14:05
  • That seems like a very poor candidate for a duplicate link for this question? The answer answers a very specific stack overflow problem, and does not in the least way possible describe how to generally track down a stack overflow problem. Commented Jan 29, 2015 at 14:06

1 Answer 1

1

EDIT: Added break, in case no condition is true.


Instead, rewrite the function using a loop. Additionally, you can simplify your logic somewhat.

Note: the assumption that both leftIndex and rightIndex are greater than 0 persists.

 private void ShowDiff(int leftIndex, int rightIndex)
 {
     while(leftIndex > 0 && rightIndex > 0)
     {
         if (leftIndex > 0 && rightIndex > 0 &&
                _compareFunc(
                    _left[_preSkip + leftIndex - 1],
                    _right[_preSkip + rightIndex - 1]))
         {
             leftIndex--;
             rightIndex--;
             FireLineUpdate(
                 DiffType.None,
                 _preSkip + leftIndex - 1,
                 -1);
         }
         else
         {
             if (rightIndex > 0 &&
                    (_matrix[leftIndex, rightIndex - 1] >=
                        _matrix[leftIndex - 1, rightIndex]))
             {
                 rightIndex--;
                 FireLineUpdate(
                     DiffType.Inserted,
                     -1,
                     _preSkip + rightIndex - 1);
             }
             else if (_matrix[leftIndex, rightIndex - 1] <
                         _matrix[leftIndex - 1, rightIndex]))
             {
                 leftIndex--;
                 FireLineUpdate(
                     DiffType.Deleted,
                     _preSkip + leftIndex - 1,
                     -1);
             }
             else
             {
                 break;
             }
         }
     }
 }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.