提升共享库中的异常
boost exceptions in a shared library
>编辑:已解决。问题要良性得多 - 我有两个函数在一行代码中相互调用 - 都使用lexical_cast
,另一个崩溃。有趣的是,我只能通过撒上很多cout
来发现这一点,因为崩溃时没有回溯,并且在调试行时,gdb 出于某种原因显示错误的lexical_cast
作为罪魁祸首(我没有看到另一个,叹息(。感谢您的帮助!
我正在使用 gcc 4.1.2 和提升 1.48。我在模板函数内的共享库中有以下代码:
try {
boost::lexical_cast<T>(s);
}
catch (...) {
std::cout << "Caught it" << std::endl;
throw;
}
强制转换失败,但异常未被捕获(它确实传播并终止程序,但此 catch-子句不会捕获它(。 T
是long
,s
是等于"234a234"
的std::string
。(我还尝试将提升包含包装在#pragma GCC visibility push(default)
中,并尝试在链接时添加-shared-libgcc
标志,但这没有任何作用。
不过它会变得更好。在以下两种情况下,确实会捕获异常:
try {
throw boost::bad_lexical_cast();
}
catch (...) {
std::cout << "Caught it" << std::endl;
throw;
}
令人惊讶的是,这个:
try {
boost::lexical_cast<T>(s);
throw boost::bad_lexical_cast();
}
catch (...) {
std::cout << "Caught it" << std::endl;
throw;
}
关于正在发生的事情的任何想法,更重要的是如何解决这个问题?
我无法在我的机器上重现,但我使用的是不同的编译器
gcc version 4.2.1 (Based on Apple Inc. build 5658) (LLVM build 2336.1.00)
我使用以下作为测试:
#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;
}
我得到以下输出:
Caught bad lexical cast with error bad lexical cast: source type value could not be interpreted as target
cast is [123456] from string [123456]
如果我删除bad_lexical_cast抓住包罗万象的作品。
Unknown exception caught!
cast is [123456] from string [123456]
也许这只是一个编译器错误? 我在提升用户列表中找到了这个
http://boost.2283326.n4.nabble.com/conversion-lexical-cast-doesn-t-throw-td2593967.html
如果BOOST_NO_EXCEPTIONS
在某处被定义,就会发生这种情况。
相关文章:
- 将成员变量添加到共享库中的类中,不会破坏二进制兼容性吗
- 处理多个异常集合的C++方法
- 我在c++代码中生成了一个运行时#3异常
- 孤立代码块在结构中引发异常
- C++中的赋值发生,尽管右侧出现异常
- 是否可以通过C++扩展强制多个python进程共享同一内存
- 在cuda线程之间共享大量常量数据
- 如何从具有移动语义的类对象中生成共享指针
- 提升进程间共享内存open_or_create每次都会引发异常
- VS10/12中的Matlab共享C/C++库抛出了许多异常
- 如何在linux x64上创建共享库对象,该对象内部使用C++异常,并且可以在旧平台上运行
- 从 Android NDK 共享对象中删除异常/展开函数
- 提升中的异常:进程间,共享内存对象删除
- 异常安全C++共享指针
- 共享库中的内部异常终止最终用户应用程序
- 异常:bad_weak_ptr同时共享from_this
- 提升共享库中的异常
- boost::创建托管共享内存对象时出现进程间ubuntu异常
- c++中共享c++库之间的异常
- 在加载的共享库中捕获异常