C++ 异常会安全地通过 C 代码传播吗?
我有一个调用 SQLite 的 C++ 应用程序(SQLite 在 C 中)sqlite3_exec() 反过来可以调用我用 C++ 实现的回调函数.SQLite 被编译成静态库.
I have a C++ application that calls SQLite's (SQLite is in C) sqlite3_exec() which in turn can call my callback function implemented in C++. SQLite is compiled into a static library.
如果异常逃脱了我的回调,它是否会通过 SQLite 的 C 代码安全地传播到调用 sqlite3_exec() 的 C++ 代码?
If an exception escapes my callback will it propagate safely through the C code of SQLite to the C++ code calling sqlite3_exec()?
推荐答案
我猜这是编译器依赖的.但是,在回调中抛出异常将是一个非常糟糕的主意.要么它完全不起作用,要么 SQLite 库中的 C 代码将无法处理它.考虑一下这是否是 SQLite 中的一些代码:
My guess is that this is compiler dependent. However, throwing an exception in the callback would be a very bad idea. Either it will flat-out not work, or the C code in the SQLite library will be unable to handle it. Consider if this is some code in SQLite:
{
char * p = malloc( 1000 );
...
call_the_callback(); // might throw an exception
...
free( p );
}
如果异常有效",C 代码将无法捕获它,并且 p 将永远不会被释放.当然,图书馆可能分配的任何其他资源也是如此.
If the exception "works", the C code has no possible way of catching it, and p will never be freed. The same goes for any other resources the library may have allocated, of course.
相关文章