exception problem with cygwin - terminate called recursively

Discussion in 'C++' started by Philipp Kraus, Jul 14, 2011.

  1. Hello,

    I have a problem with exception. I compile my code under OSX, Linux and
    Windows (Cygwin) with gcc but only in Windows I run everytime in a
    exception message "terminate called recursively". My code runs into an
    exception, but I catch them with try-catch. The code unter Linux and
    OSX do it in the correct, catch the exception and runs down till the
    end of the program, but under Windows the exception is thrown and the
    program terminates.

    Can anyone explain me the problem of the message, I don't understand
    why Windows handels exceptions in another way than Linux or OSX

    Thanks

    Phil
     
    Philipp Kraus, Jul 14, 2011
    #1
    1. Advertising

  2. On 2011-07-14 17:11:25 +0200, peter koch said:

    > It seems to me that your exception either has no corresponding catch
    > clause or that it is thrown when another exception is active: this
    > causes std::terminate to be called.


    on the main program the exception should be catched like:

    myclass {
    mymethod() {
    if something
    mymethod()

    throw exception
    }
    }

    main() {
    myclass x;
    try {
    x.mymethod()
    } catch (...) {}

    }

    In my optinion I think if I throw an exception the complete runing
    process would be breaked down and the exception is send to the main
    program, if there is a try-catch the exception is be handled, if not
    the program terminates. I don't use any threads. I don't understand why
    Windows should be handle the exception in another way than Linux and
    OSX !?

    Thanks for your answer

    Phil
     
    Philipp Kraus, Jul 14, 2011
    #2
    1. Advertising

  3. On 2011-07-14 18:00:47 +0200, peter koch said:

    > Den torsdag den 14. juli 2011 17.48.24 UTC+2 skrev Philipp Kraus:
    >> On 2011-07-14 17:11:25 +0200, peter koch said:
    >>
    >>> It seems to me that your exception either has no corresponding catch
    >>> clause or that it is thrown when another exception is active: this
    >>> causes std::terminate to be called.

    >>
    >> on the main program the exception should be catched like:
    >>
    >> myclass {
    >> mymethod() {
    >> if something
    >> mymethod()
    >>
    >> throw exception
    >> }
    >> }
    >>
    >> main() {
    >> myclass x;
    >> try {
    >> x.mymethod()
    >> } catch (...) {}
    >>
    >> }
    >>

    >
    > So you tried that program and it had the same behaviour?


    yes

    >
    >> In my optinion I think if I throw an exception the complete runing
    >> process would be breaked down and the exception is send to the main
    >> program, if there is a try-catch the exception is be handled, if not
    >> the program terminates. I don't use any threads. I don't understand why
    >> Windows should be handle the exception in another way than Linux and
    >> OSX !?

    >
    > It does not.
    >
    >>
    >> Thanks for your answer
    >>

    >
    > If there is not a corresponding catch, std::terminate will be called.
    > You said that std::terminate was called recursively?


    Sorry, my mistake. The exception that is thrown shoudl be called recursively.

    > Your problem might also be in the compiler flags used for the compiler.


    I use on all systems the gcc, but I don't know any flags that are set
    for exception handling
    I'm a little bit confused

    Thanks

    Phil
     
    Philipp Kraus, Jul 14, 2011
    #3
  4. I have used gdb and debugging symbols for debugging my code. It seems
    that the program crashs at

    #0 0x77bf70b4 in ntdll!LdrFindResourceEx_U () from
    C:\Windows\SYSTEM32\ntdll.dll
     
    Philipp Kraus, Jul 14, 2011
    #4
  5. On 2011-07-14 18:00:47 +0200, peter koch said:

    > Your problem might also be in the compiler flags used for the compiler.


    Do you mean -fexceptions for the g++ command?

    I'm a little bit closer on the problem context, I have mixed C and C++
    code in the class that throws the exception. I have tested the code
    with std exceptions and try to catch it with
    catch (...), catch (myexception& e) or catch(std::runtime_error e), but
    I cant catch the thrown exception. Is there any information about
    exception handling with mixed code?
    I have found some articles that there are problems with mixed code and
    exception handling, but I haven't found a working solution for the
    problem. I try to recompile my sources with the -fexceptions flag, but
    that does not wort. Shoud I compile my C libraries with the flag?

    Thanks a lot

    Phil
     
    Philipp Kraus, Jul 16, 2011
    #5
  6. On 2011-07-16 18:04:37 +0200, Paavo Helde said:

    > Philipp Kraus <> wrote in news:ivs5ac$c5s$1
    > @online.de:
    >
    >> I'm a little bit closer on the problem context, I have mixed C and C++
    >> code in the class that throws the exception.

    > [...]
    >> problem. I try to recompile my sources with the -fexceptions flag, but
    >> that does not wort. Shoud I compile my C libraries with the flag?

    >
    > The -fexceptions flag should not have any effect on C++ code whatsoever.
    > You need this for compiling any C code which is supposed to let C++
    > exceptions propagate through it. On Windows platform MSVC implements
    > exceptions at the OS level so there is no such special flag needed for C
    > code, but things may be different with cygwin or gcc.


    I think this question is off-topic, but do you know a working solution for g++?
    I have compiled all my libraries and my code with g++ / gcc, so should
    I set the
    -fexceptions flag on each library or only on my code?

    Thanks

    Phil
     
    Philipp Kraus, Jul 16, 2011
    #6
  7. On 2011-07-16 22:26:34 +0200, Paavo Helde said:

    > Philipp Kraus <> wrote in
    > news:ivsj85$njn$:
    >
    >> On 2011-07-16 18:04:37 +0200, Paavo Helde said:
    >>
    >>> Philipp Kraus <> wrote in news:ivs5ac$c5s$1
    >>> @online.de:
    >>>
    >>>> I'm a little bit closer on the problem context, I have mixed C and
    >>>> C++ code in the class that throws the exception.
    >>> [...]
    >>>> problem. I try to recompile my sources with the -fexceptions flag,
    >>>> but that does not wort. Shoud I compile my C libraries with the
    >>>> flag?
    >>>
    >>> The -fexceptions flag should not have any effect on C++ code
    >>> whatsoever. You need this for compiling any C code which is supposed
    >>> to let C++ exceptions propagate through it. On Windows platform MSVC
    >>> implements exceptions at the OS level so there is no such special
    >>> flag needed for C code, but things may be different with cygwin or
    >>> gcc.

    >>
    >> I think this question is off-topic, but do you know a working solution
    >> for g++? I have compiled all my libraries and my code with g++ / gcc,
    >> so should I set the
    >> -fexceptions flag on each library or only on my code?

    >
    > I don't know if this is off-topic or not, there is too less information.
    > For example it is not clear what you mean exactly by "mix of C and C++ in
    > the class" and if "your code" is C or C++. Maybe you could post a minimal
    > example demonstrating the problem?


    It's very simple. I use the libxml2 functions for parsing XML data. The library
    have some error function / callback function for detecting errors
    during the parsing.

    eg:

    my xmlclass {
    public:
    doxml( std::string p_xml ) {

    xmlParseMemory(p_xml.c_str)
    if (xmlErrorDetect()) {
    xmlClear()
    throw std::runtime_error("error")
    }
    }
    }

    In my main function
    {
    myxmlclass obj;
    try {
    obj.doxml("abcdef");
    } catch (...) {
    std::cout << "exception is caught" << std::endl;
    }
    }


    The calls with xml prefix are the calls of the libxml2. I have checked
    with gdb, the throw will be called, but not caught in the main. I can
    change
    the (...) to anything, but I can't catch the exception. I have tested
    with std::exception, own exception, that derivated from std::exception.
    Under
    the Cygwin g++ I can't catch it, same code under OSX and Linux works
    fine, the exception is caught and I can handle it.

    > All I know that with Linux and gcc, all C code which has to be able to
    > propagate exceptions through it must be compiled with -fexceptions.


    The -fexceptions is enabled on default, so I think its correct.

    I do not understand where the problem is and how I can solve it
     
    Philipp Kraus, Jul 16, 2011
    #7
  8. On 2011-07-17 00:39:51 +0200, Paavo Helde said:

    > Philipp Kraus <> wrote in
    > news:ivt1ke$3k4$:
    >
    >> On 2011-07-16 22:26:34 +0200, Paavo Helde said:
    >>
    >>> Philipp Kraus <> wrote in
    >>> news:ivsj85$njn$:
    >>>
    >>>> On 2011-07-16 18:04:37 +0200, Paavo Helde said:
    >>>>
    >>>>> Philipp Kraus <> wrote in
    >>>>> news:ivs5ac$c5s$1 @online.de:
    >>>>>
    >>>>>> I'm a little bit closer on the problem context, I have mixed C and
    >>>>>> C++ code in the class that throws the exception.
    >>>>> [...]
    >>>>>> problem. I try to recompile my sources with the -fexceptions flag,
    >>>>>> but that does not wort. Shoud I compile my C libraries with the
    >>>>>> flag?
    >>>>>
    >>>>> The -fexceptions flag should not have any effect on C++ code
    >>>>> whatsoever. You need this for compiling any C code which is
    >>>>> supposed to let C++ exceptions propagate through it. On Windows
    >>>>> platform MSVC implements exceptions at the OS level so there is no
    >>>>> such special flag needed for C code, but things may be different
    >>>>> with cygwin or gcc.
    >>>>
    >>>> I think this question is off-topic, but do you know a working
    >>>> solution for g++? I have compiled all my libraries and my code with
    >>>> g++ / gcc, so should I set the
    >>>> -fexceptions flag on each library or only on my code?
    >>>
    >>> I don't know if this is off-topic or not, there is too less
    >>> information. For example it is not clear what you mean exactly by
    >>> "mix of C and C++ in the class" and if "your code" is C or C++. Maybe
    >>> you could post a minimal example demonstrating the problem?

    >>
    >> It's very simple. I use the libxml2 functions for parsing XML data.
    >> The library have some error function / callback function for detecting
    >> errors during the parsing.

    >
    > You mention a callback function, but there is no callback function in
    > your example code. Do you use xmlSetGenericErrorFunc()? If yes, then this
    > should be included in the example.
    >
    >> eg:
    >>
    >> my xmlclass {
    >> public:
    >> doxml( std::string p_xml ) {
    >>
    >> xmlParseMemory(p_xml.c_str)
    >> if (xmlErrorDetect()) {
    >> xmlClear()
    >> throw std::runtime_error("error")
    >> }
    >> }
    >> }
    >>
    >> In my main function
    >> {
    >> myxmlclass obj;
    >> try {
    >> obj.doxml("abcdef");
    >> } catch (...) {
    >> std::cout << "exception is caught" << std::endl;
    >> }
    >> }

    >
    > This code does not compile (lots of missing semicolons and other errors),
    > neither does it throw exceptions through C code, so I believe it has no
    > relevance to your real problem. Please post a compilable example
    > demonstrating the problem!


    The original excerpt of the method is:

    xmlResetLastError();
    bool l_error = false;

    xmlGenericErrorFunc l_fptr = myclass::XMLErrorFunction; <=
    XMLErrorFunction is a static
    method of the class which is empty to supress parsing content on the CLI
    initGenericErrorDefaultFunc( &l_fptr );

    const char* l_xmlcontent = p_xml.c_str();
    xmlDocPtr l_xml = xmlParseMemory( l_xmlcontent,
    strlen(l_xmlcontent) );
    if ((!l_xml) || (xmlGetLastError())) {
    if (l_xml)
    xmlFreeDoc( l_xml );
    xmlCleanupParser();
    throw std::runtime_error("XML data can not be parsed"); <=
    this exception is thrown, but can not be caught /
    here I have tested with std::exception, derivated exceptions...
    }


    > AFAIK -fexceptions are not enabled by default for C code, are you sure
    > that the libxml2 library has been compiled with -fexceptions?


    I have testet with and without compiling -fexceptions, both test are
    create the same problem.

    > You cannot solve the problem without understanding it. Throwing
    > exceptions through non-C++ code is undefined behavior, but your example
    > does no such thing, so I'm not sure either where the problem is.


    I have create a class, that uses with external C includes the libxml2
    calls for parsing XML data. In this class I throw an
    exception (see excerpt), which should be caught in the main. On Linux
    and OSX g++ it works, exception is thrown and caught.
    On Windows (g++ under Cygwin) the exception is thrown (checked with
    gdb) but not caught. Windows creates the message:
    terminate called after throwing an instance of <exception> terminate
    called recursively
    The library is compiled on all three systems with gcc and my code with
    g++. All compilers are version 4.x, only the subversions
    are different

    Phil
     
    Philipp Kraus, Jul 17, 2011
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mohsen
    Replies:
    14
    Views:
    14,009
    Nate Barney
    Nov 8, 2006
  2. peter koch
    Replies:
    1
    Views:
    353
    Ian Collins
    Jul 14, 2011
  3. Philipp Kraus
    Replies:
    0
    Views:
    610
    Philipp Kraus
    Jul 17, 2011
  4. Daniel Fetchinson
    Replies:
    0
    Views:
    195
    Daniel Fetchinson
    Oct 2, 2012
  5. Peter Otten
    Replies:
    0
    Views:
    194
    Peter Otten
    Oct 2, 2012
Loading...

Share This Page