Stack overflow uncaught by stdlib's debug code.

Hi there. Please excuse my relative lameness or unawareness, but here it goes (I know it's wrong):

Visual C++ 2005 Professional, Win32 Console Application ran in Debug mode, with all flags set to defaults, except for warnings, which are set to /w4. Launched from devenv with ctrl-f5 (menu - Debug -> Start Without Debugging)


#include <iostream>
using namespace std;

int InfiniteRecursion(int i)
{
    return InfiniteRecursion(i + 1);
}

int main()
{
    cout << "before attack...\n";
    cout  << InfiniteRecursion(0) << endl;
    cout << "...after attack\n";
}

 


The compiler is smart enough and notifies me about infinite recursion in the code, but the output I get in the console is:


before attack...
Press any key to continue . . .

 

I do not get any warning/error dialogs as I used to get in prior versions of VC I used to test (2003, 6). Of course, my example is trivial and easy to track, but it took me over an hour to debug a more subtle number sorting routine, which in debug mode didn't do much, while in release mode it worked flawlessly.
By the way, if I run the above code with menu - Debug - Start Debugging (F5) I get a dialog with Unhandled exception, which is what I expect when I run it without debugger too.

Any ideas Thanx in advance.



Answer this question

Stack overflow uncaught by stdlib's debug code.

  • *******

    Thank you.

    Only thing is that I was coding a QuickSort algo among other pieces of code and I only thought that my general algorythms were bugged until I received the familiar unhandled exception from Visual Studio 2003 which led me to the conclusion that the recursion in my quicksort went beyound the 1megabyte debug stack. I considered it's a bug either in the library from 2005 or in the IDE (since 2003 reports it correctly). But now I see that if i launch the app with F5 (go with debugger) it also catches the exceptions, so I'll probably always launch with the debugger from now on.

    Keep up the good work guys! and never let this excellent compiler die to c#'s.


  • Aur&amp;#233;lien

    The application does actally crash on C++. You can verify this by running the executable on the comand line and check the return value. It will be 128 which indicates an AV:
    cl /EHa /FAsc sample.cpp
    sample.cpp
    sample.cpp(7) : warning C4717: 'InfiniteRecursion' : recursive on all control
    paths, function will cause runtime stack overflow

    r.exe
    before attack...
    echo %errorlevel%
    128

    The asm generate looks correct as well:

    InfiniteRecursion@@YAHH@Z PROC    ; InfiniteRecursion
    ; 5    : { 
    00000 55   push  ebp
      00001 8b ec   mov  ebp, esp
    ; 6    :     return InfiniteRecursion(i + 1);
      00003 8b 45 08  mov  eax, DWORD PTR _i$[ebp]
      00006 83 c0 01  add  eax, 1
      00009 50   push  eax
      0000a e8 00 00 00 00  call  InfiniteRecursion@@YAHH@Z ; InfiniteRecursion
      0000f 83 c4 04  add  esp, 4
    ; 7    : }
      00012 5d   pop  ebp
      00013 c3   ret  0


    If you modify you code to be something like:
    #include <iostream>
    using namespace std;
    int InfiniteRecursion(int i)
    {
        return InfiniteRecursion(i + 1);
    }
    int main()
    {
        cout << "before attack...\n";
        try {
        cout  << InfiniteRecursion(0) << endl;
     }
     catch (...)
     {
      cout<< "Caught Exception"<<endl;
     }
        cout << "...after attack\n";
        return 0;
    }

    The output of compiling then running (cl /EHa sample.cpp):
    before attack...
    Caught Exception
    ...after attack


    Note: You will have to use /EHa to catch such exceptions or else the application will just crash as if you didn't add any try catch statments.

    Thanks,
      Ayman Shoukry
      VC++ Team



  • Mr. Jeff King

    Just wrote this in c# (I don't know c#):


    using System;
    using System.Collections.Generic;
    using System.Text;

    namespace ConsoleApplication1
    {
        class Program
        {
            static int InfiniteRecursion(int a)
            {
                return InfiniteRecursion(a + 1);
            }

            static void Main(string[] args)
            {
                System.Console.WriteLine("starting...");
                System.Console.Out.Write(InfiniteRecursion(0));
                System.Console.WriteLine("...stopped");
            }
        }
    }

     


    Same console application, ran it in exactly the same circumstances and ended up with a dialog about an unhandled exception (which is what i've expected from the c++ version too).


  • Stack overflow uncaught by stdlib's debug code.