C++ 未被销毁的例外情况

C++ 未被销毁的例外情况,c++,exception,gcc,exception-handling,g++,C++,Exception,Gcc,Exception Handling,G++,我正在尝试实现我的\uuucxa\uAllocate\uException和\uucxa\uFree\uException版本,以避免在抛出时分配内存 因此,我实现了一个内存池,它似乎工作得很好。但是当使用嵌套异常进行测试时,在所有情况下都不会调用异常的析构函数,因此也不会调用\uuucxa\u free\u exception,导致内存池随着时间的推移而填满 请参见以下示例代码: class MyException { public: MyException() { std::co

我正在尝试实现我的
\uuucxa\uAllocate\uException
\uucxa\uFree\uException
版本,以避免在
抛出时分配内存

因此,我实现了一个内存池,它似乎工作得很好。但是当使用嵌套异常进行测试时,在所有情况下都不会调用异常的析构函数,因此也不会调用
\uuucxa\u free\u 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;
  }
}
它第一次工作正常。但在此之后,在每次循环迭代中构造和分配两个异常,但只有一个异常被释放和销毁


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

以类似的方式分配正确的内存量,为我修复崩溃:

#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;
  }
}
#包括
#包括
#包括
#包括
类MyException{
公众:
MyException(){

Std::Cuth

我认为这个问题的简短答案是它未被指定。C++标准的一部分有这样的说法:

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

比如MSVC,祝你好运

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

如果您查看
\ucxa\u allocate\u exception
(第279行)的实现,您将看到它做了三件您没有做的事情:

  • 它为(私有)类型的头分配额外的空间
    \ucxa\u refcounted\u exception
  • 它将该标题归零
  • 它返回指向该头后第一个字节的指针
然后,在
\uucxa\u free\u exception
中,它允许在释放指针之前进行指针调整

因此,让它工作起来很容易,只需做这样的事情(或者你可以通过隧道进入
\uucxa\urefcounted\u exception
的声明,我想它就在那个网站的某个地方):


不过,他们一定是用不同的方式做事。就像我说的,这是UB,所以要小心。

看看这个,根本没有破坏发生,即使是第一次!在我的测试中,这只是崩溃,这是你实际上被允许做的吗?是的,我也崩溃了。查看libstdc++源代码,你需要分配更多than
shown\u size
我想说这只是UB,因为你用保留标识符覆盖了一个库实现细节。理解它为什么以这种特殊方式失败可能会很有趣,但我并不觉得奇怪它不起作用。看起来你比我更厉害了……我没有看到这一点,我正在键入我的。Th谢谢!我试图将您的解决方案集成到我的内存池()但破坏仍然不能正常工作。有什么想法吗?@AndreasPasternak你做得不对。事实上,据我所知,你根本没有做,这是正确的链接吗?@PaulSanders:我犯了一个小错误,但现在它工作了。谢谢你的帮助。这里的工作版本:不幸的是,我不能接受两个答案,因此我接受了他说得更详细了。不过谢谢你们两位的帮助!这里有一些相关的讨论
#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;
  }
}
#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);
}
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
-----------