异常未被销毁

时间:2018-06-28 07:23:33

标签: 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。

2 个答案:

答案 0 :(得分:3)

我认为对此的简短答案是未指定。 C ++标准的18.1.4部分规定:

  

异常对象的内存以未指定的方式分配...

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

但是,有趣的是,为什么编写的代码失败(对于其他注释程序,gcc在我尝试运行它时报告内存损坏),正如@AlanBirtles所说,答案就在这里:

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

如果查看IF/ELSE的实现(第279行),您会发现它执行了三项您没有做的事情:

  • 它为(专用)类型__cxa_allocate_exception的标头分配了额外的空间
  • 将标头清零
  • 它返回指向该标头之后的第一个字节 的指针

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

因此,使它正常工作很容易,只需执行以下操作即可(或者您可以通过自己的方式访问__cxa_free_exception的声明,我认为它位于该站点的某个位置):

__cxa_refcounted_exception

当我run this at Wandbox时,我得到:

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

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

答案 1 :(得分:1)

以类似于libstdc++的方式分配正确的内存量为我解决了崩溃问题:

#include <iostream>
#include <cstdlib>
#include <exception>
#include <cstring>

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

const size_t __cxa_refcounted_exception_size = 16 * sizeof(size_t); // approx sizeof(__cxa_refcounted_exception)

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

void __cxa_free_exception(void *thrown_object)
{
  std::cout << "free: " << thrown_object << std::endl;
  char *ptr = (char *) thrown_object - __cxa_refcounted_exception_size;

  free(ptr);
}

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

int main() {
  for (int i=0;i<4;i++) {
    non_rec();
    std::cout << "-----------" << std::endl;
  }
}

您应该通过包含unwind-cxx.h来找到平台的sizeof(__cxa_refcounted_exception_size)的实际值。

相关问题