- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我正在尝试实现我的 __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 行),您会发现它做了三件您没有做的事情:
__cxa_refcounted_exception
然后,在 __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/
当你有一个对象 OBJ1 包含一个关键部分 CS 和一个指向另一个对象 OBJ2 的指针时,我无法弄清楚什么是正确的关闭过程 假设您有两个函数 A 和 B。 A进入临界区,修改OBJ2中的数据后离开临
我有一个成员变量声明为 CComPtr m_spXMLDoc; XML 文档是这样创建的 CoCreateInstance(CLSID_DOMDocument, NULL, CLSCTX_INPROC
在我的工作平台中,我遇到了 session_destroy 问题 function logout() { $_SESSION['id'] = ''; session_destroy(); } 在这
如何使用 destroy 删除 jScrollPane。请您给出以下代码的简单示例: $(document).ready(function() { $(".div1").jScrollPane
这是我在这里发表的第一篇文章,但我想对社区表示感谢,因为通过来到这里并在已经得到解答的问题中找到解决方案,我已经无数次找到了问题的解决方案。 话虽这么说,我想进入正题。我正在 Code Gear 的
我在导航 View 中工作。在此导航 View 中,您可以偶然发现个人资料页面。在此个人资料页面中,您可以看到与当前个人资料相关的其他个人资料(基本上是数据 View 中显示的图片)。您可以通过点击这
我想销毁项目中的 session ,这样当我单击“注销”时,它会转到页面“KillSession.jsp”,在该文件中我编写了“session.invalidate();”然后我将用户重定向到登录页面
我有关于 Thread 的 2 问题,我只是想澄清一些事情。使用以下代码: public class MyThread implements Runnable { Boolean StopTh
我遇到了内存泄漏,代码类似于下面的代码(这是每个循环都有不同输入的模拟)。 问题 对象 Object_XXX 非常复杂,它与数据库以及其他填充了数据库数据的对象有连接。 for(int i=0
当我在 Python 中启动一个类时,我给它一些值。然后我调用类中执行某些操作的方法。这是一个片段: class TestClass(): def __init__(self):
我想删除所有 div、类、属性和几乎所有 CKEDITOR 添加到 DOM 的内容。例如调用 jquery tabs("destroy");将删除所有由 jQuery 选项卡添加的 div。我怎样才能
我想清除析构函数中的一个映射,但我不知道它是否为空。如果我按如下方式清除它可以吗? for(std::map::iterator it = m_map.begin(); it != m_map.end
我正在尝试克隆 TikTok 应用。对于主屏幕,我制作了一个 VerticalViewPager(自定义 View 分页器),其中包含“点赞”按钮、标题和评论。我正在从 Firebase 检索视频。
我正在制作自己的游戏。目标之一是在世界中拥有尽可能多的物体。在这个游戏中,需要在一些不可预测的时间段内创建许多对象(比如武器开火会创建一个对象),一旦该弹丸击中某物,该对象也需要被摧毁(也许它击中的东
有没有办法在 JavaScript 中破坏 HTML5 WebWorker? 这是我的情况:我有一个 Web 应用程序生成相当数量的 WebWorker(在 16 到 32 之间的任何地方)来优化一些
如何销毁 php 中的 session ? 事情是当用户点击注销按钮时, session 将结束,他将被重定向到 index.php 这是我的代码 客户.php 这是来自用户想要再次登录的 ind
关于 GtkBuilder 的问题。 当我们取消引用构建器指针时,它是否会破坏构建器创建的所有屏幕/小部件? if( builder_ptr ) g_object_unref(G_OBJECT
有没有办法销毁 WebView 实例?如果页面加载,并说视频开始播放,我希望能够,当我隐藏 WebView 时,基本上可以销毁它,或者至少重置它。 我知道我可以听 visibleProperty 并执
我有一组可拖动的元素。如何删除可拖动功能? $('.draggable').draggable('disable') 在我的情况下不是一个选项 $('.draggable').draggable('d
下面的代码会抛出一个 EZDecompressionError 消息 'Invalid ZStream operation' 每当行 Reader.Free 被执行。有人可以告诉我这段代码有什么问题吗
我是一名优秀的程序员,十分优秀!