c++ - 异常未被销毁

标签 c++ exception gcc exception-handling g++

我正在尝试实现我的 __cxa_allocate_exception__cxa_free_exception 版本以避免在 throw 上分配内存。

所以我实现了一个似乎工作正常的内存池。但是在测试嵌套异常时,异常的析构函数并没有在所有情况下被调用,因此 __cxa_free_exception 也没有被调用,导致内存池随着时间的推移而被填满。

参见下面的示例代码:

class MyException {
public:
  MyException() {
    std::cout << "MyException constructed." << std::endl;
  }
  ~MyException() {
    std::cout << "MyException destroyed." << std::endl;
  }
};

void * __cxa_allocate_exception(size_t thrown_size)
{
  const auto mem = malloc(thrown_size); //Not part of the example
  std::cout << "allocate: " << mem <<  std::endl;
  return mem;
}

void __cxa_free_exception(void *thrown_object)
{
  std::cout << "free: " << thrown_object << std::endl;

  free(thrown_object); //Not part of the example.
}

void non_rec() {
  try {
    throw MyException();
  } catch(...) {
    try {
      throw MyException();
    } catch(...) {
      //...
    }
  }
}

int main() {
  while(true) {
    non_rec();
    std::cout << "-----------" << std::endl;
  }
}

这个程序的输出是:

allocate: 0x8cbc20
MyException constructed.
allocate: 0x8cc030
MyException constructed.
MyException destroyed.
free: 0x8cc030
MyException destroyed.
free: 0x8cbc20
-----------
allocate: 0x8cbc20
MyException constructed.
allocate: 0x8cc030
MyException constructed.
MyException destroyed.
free: 0x8cc030
-----------
allocate: 0x8cc030
MyException constructed.
allocate: 0x8cc440
MyException constructed.
MyException destroyed.
free: 0x8cc440
-----------
allocate: 0x8cc440
MyException constructed.
allocate: 0x8cc850
MyException constructed.
MyException destroyed.
free: 0x8cc850

它第一次正常工作。但在那之后,在每次循环迭代中构造和分配两个异常,但只有一个被释放和销毁。

我在 Ubuntu 16.04 上使用 g++ 5.4.0。

最佳答案

我认为对此的简短回答是它未指定。科18.1.4 C++ 标准是这样说的:

The memory for the exception object is allocated in an unspecified way...

MSVC,例如 allocates it on the stack .祝你好运。

然而,有趣的是研究为什么编写的代码失败(对于其他评论员,当我尝试运行它时 gcc 报告内存损坏)并且正如@AlanBirtles 所说,答案就在这里:

https://code.woboq.org/gcc/libstdc++-v3/libsupc++/eh_alloc.cc.html

如果您查看 __cxa_allocate_exception 的实现(第 279 行),您会发现它做了三件您没有做的事情:

  • 它为(私有(private))类型的 header 分配额外空间 __cxa_refcounted_exception
  • 它将那个标题清零
  • 它返回一个指针,指向该 header 之后的第一个字节

然后,在 __cxa_free_exception 中,它允许在释放之前调整指针。

所以让它工作很容易,只需做这样的事情(或者你可以通过隧道找到 __cxa_refcounted_exception 的声明,我想它在那个网站的某个地方):

#define EXTRA 1024

extern "C" void * __cxa_allocate_exception(size_t thrown_size)
{
  void *mem = malloc (thrown_size + EXTRA);
  std::cout << "allocate: " << mem <<  " (" << thrown_size << ") " << std::endl;
  memset (mem, 0, EXTRA);
  return (char *) mem + EXTRA;
}

extern "C" void __cxa_free_exception(void *thrown_object)
{
  std::cout << "free: " << thrown_object << std::endl;
  char *mem = (char *) thrown_object;
  mem -= EXTRA;
  free (mem);
}

当我run this at Wandbox ,我得到:

allocate: 0x1e4c990 (1) 
MyException constructed.
allocate: 0x1e4ddb0 (1) 
MyException constructed.
MyException destroyed.
free: 0x1e4e1b0
MyException destroyed.
free: 0x1e4cd90
-----------
allocate: 0x1e4c990 (1) 
MyException constructed.
allocate: 0x1e4ddb0 (1) 
MyException constructed.
MyException destroyed.
free: 0x1e4e1b0
MyException destroyed.
free: 0x1e4cd90
-----------
allocate: 0x1e4c990 (1) 
MyException constructed.
allocate: 0x1e4ddb0 (1) 
MyException constructed.
MyException destroyed.
free: 0x1e4e1b0
MyException destroyed.
free: 0x1e4cd90
-----------

doesn't work with clang但是,所以他们必须以不同的方式做事。就像我说的,这是 UB,所以要小心。

关于c++ - 异常未被销毁,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51076989/

相关文章:

python - 在 Py_BuildValue 中返回变量字符串

ruby-on-rails - 是什么导致了这个 rails ioerror closed stream?

java - 在我的应用程序中,为什么 readInt() 总是抛出 EOFException?

java - 使用现有的 try/catch block 或重复的 catch block 而不引发异常

c++ - 我可以使用本地声明的枚举类作为模板非类型参数吗? (gcc 给出模糊的错误)

c++ - 我可以在 cblas_gemm(...) 中传递与 "C"和 "A"矩阵相同的矩阵指针吗?

C++ 从另一个类访问类成员

c++ - 如何使用广度优先搜索确定是否可以在有向图中到达顶点

c - 增加堆栈大小 : typical issues?

c - 在单个参数函数中传递带括号的两个值