Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

boost exceptions in a shared library

EDIT: RESOLVED. The problem was much more benign - I had two functions that were called inside one another in one line of code - both used lexical_cast and the other one was crashing. It's interesting that I was only able to find this out by sprinkling in a lot of cout's as there was no backtrace upon crash and when debugging line be line, gdb was for whatever reason showing the wrong lexical_cast as the culprit (and I didn't see the other one, sigh). Thanks for the help!


I'm using gcc 4.1.2 and boost 1.48. I have the following code in a shared library inside a template function:

try {
  boost::lexical_cast<T>(s);
}
catch (...) {
  std::cout << "Caught it" << std::endl;
  throw;
}

The cast fails, but the exception doesn't get caught (it does propagate and terminate the program, but this catch-clause doesn't catch it). T is long and s is a std::string equal to "234a234". (I also tried wrapping the boost includes in #pragma GCC visibility push(default) and also tried adding -shared-libgcc flag when linking, and that didn't do anything.)

It gets better though. In the following two cases the exception DOES get caught:

try {
  throw boost::bad_lexical_cast();
}
catch (...) {
  std::cout << "Caught it" << std::endl;
  throw;
}

and amazingly this one:

try {
  boost::lexical_cast<T>(s);
  throw boost::bad_lexical_cast();
}
catch (...) {
  std::cout << "Caught it" << std::endl;
  throw;
}

Any ideas on what's going on and more importantly how to fix this?

like image 441
eddi Avatar asked May 15 '26 04:05

eddi


1 Answers

I'm not able to reproduce on my machine but I'm using a different compiler

gcc version 4.2.1 (Based on Apple Inc. build 5658) (LLVM build 2336.1.00)

I used the following as a test:

#include <iostream>
#include <boost/lexical_cast.hpp>
#include <string>
#include <exception>

using namespace std;

template<typename T>
T printLexicalCast(const std::string& s){
    T t;
    try {
         t = boost::lexical_cast<T>(s);
         cout << "cast is [" << t << "] from string [" << s << "]" << endl;
    }
    catch (const boost::bad_lexical_cast& e ) {
      std::cout << "Caught bad lexical cast with error " << e.what() << std::endl;
    }
    catch( ... ){
        std::cout << "Unknown exception caught!" << endl;
    }
    return t;
}   


int main(int argc, char *argv[]) {

    std::string badString("234a234");
    long l1 = printLexicalCast<long>(badString); //exception


    std::string goodString("123456");
    long l2 = printLexicalCast<long>(goodString); 

    return 0;
}

I get the following output:

Caught bad lexical cast with error bad lexical cast: source type value could not be interpreted as target
cast is [123456] from string [123456]

If I remove the bad_lexical_cast catch the catch-all works.

Unknown exception caught!
cast is [123456] from string [123456]

Maybe this is just a compiler bug? I found this on the boost-users list

http://boost.2283326.n4.nabble.com/conversion-lexical-cast-doesn-t-throw-td2593967.html

like image 144
Joel Avatar answered May 19 '26 02:05

Joel



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!