C ++异常会通过C代码安全地传播吗? [英] Will C++ exceptions safely propagate through C code?

查看:118
本文介绍了C ++异常会通过C代码安全地传播吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个C ++应用程序调用 SQLite (SQLite在C中) sqlite3_exec(),这反过来可以调用我在C ++中实现的回调函数。 SQLite被编译成一个静态库。



如果异常转义我的回调,它将通过SQLite的C代码安全传播到调用sqlite3_exec()的C ++代码? p>

解决方案

我的猜测是这是编译器依赖的。但是,在回调中抛出异常将是一个非常糟糕的主意。否则它将不能正常工作,否则SQLite库中的C代码将无法处理它。考虑这是否是SQLite中的一些代码:

  {
char * p = malloc(1000);
...
call_the_callback(); //可能会抛出一个异常
...
free(p);
}

如果异常工作,C代码没有可能的方法它和p永远不会被释放。当然,图书馆可能分配的任何其他资源也一样。


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.

If an exception escapes my callback will it propagate safely through the C code of SQLite to the C++ code calling sqlite3_exec()?

解决方案

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 );
}

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.

这篇关于C ++异常会通过C代码安全地传播吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆