AnsweredAssumed Answered

MCUX 10.2.1 - C++ exceptions not being caught

Question asked by David Rodgers on Nov 5, 2018
Latest reply on Nov 13, 2018 by David Rodgers

I've recently encountered an issue with my Kinetis K24 C++ project where it appears that unknown conditions cause my project to build in such a way that it's impossible to catch exceptions, and I don't know what's causing this "fragility".

 

Here's my test code:

  static void VerifyExceptions(void) {
    try {
      throw std::runtime_error("Exceptions are being handled normally.");
    } catch (std::runtime_error &e) {
      std::cout << e.what() << "\n";
    }
    /* If exceptions are not working correctly, then the above statement will
     * cause __cxa_throw() to call terminate() immediately.  Testing this at
     * startup assures that any issues with exceptions will be immediately
     * diagnosed during development. */
  }

 

I have a call to VerifyException() near the top of my main() function, after I've initialized the console.  Normally it prints out the test message and continues.  I had been developing quite happily the last week or so, then I committed some updates to one of my program modules yesterday and tested out the code.  Suddenly, I'm getting this on my console at startup:

terminate called after throwing an instance of 'std::runtime_error'
terminate called recursively

This is what my call stack looks like:

Exception call stack leading to terminate(), abort(), exit()

I had first encountered this issue maybe a week or two ago.  I have a test module where I can press a couple of keys and send a JSON string to a parser to be processed.  The parser is written in C++ and uses exceptions, which my application catches.  Somehow, adding one additional test case to my app caused ALL C++ exceptions to fail in this manner, by removing that one test case from my module, the fault went away.  I had hoped that was the end of it, but apparently not.  I put in this self-check of exception handling as a mineshaft canary for just this reason.

 

I'm building my app with Newlib, not Newlib-Nano, since Nano was built with exceptions disabled (for significant code size savings).  This is my second project using C++ and JSON parsing on the K24/64; my first was done two years ago on Kinetis Design Studio, and I never had any issues with catching exceptions.  What concerns me is how random this issue appears to be.  With the code and project in a given state, the executable will build the same way each time, even on another developer's machine (using the same version of MCUXpresso).  So it's not tied to my particular workstation; it's tied purely to the code as it exists at that moment.

 

Can anyone (NXP or otherwise) explain to me how exception handling could be sabotaged globally simply by the particular object modules that are being built?  (If NXP wants to do a ticket-based investigation, I can send our code privately.) Thanks.

 

(Win 10 Pro 1803, MCUX 10.2.1, C++11 and C11, linking with Newlib, -fexceptions specified in compiler flags)

Outcomes