gpt4 book ai didi

c++ - 如何在程序退出之前强制 boehm gc 全部收集

转载 作者:太空狗 更新时间:2023-10-29 21:24:11 70 4
gpt4 key购买 nike

我现在正在学习 boehm gc c++ 接口(interface)。不使用它,只是好奇。我按照官方的例子,写我的类什么可以被 GC,在析构函数中,有一个输出,所以我可以判断一个实例是否被 GC。但只有当我使用循环创建多达一千个或更多的实例时,才会触发 gc 收集。如果代码像这样简单:

class test_gc : public gc
{
public:
~test_gc()
{
cout << "~test_gc()" << endl;
}
};

int main()
{
GC_INIT();
::new (GC) test_gc();
GC_gcollect();
return 0;
}

似乎从未调用过析构函数。没有输出“~test_gc()”。编译器的优化也被关闭。我做了一些谷歌,除了官方示例几乎什么都找不到。

请帮助我如何在程序退出前强制执行 gc 收集,或者告诉我如果我使用错误如何正确使用它。非常感谢。

最佳答案

I did some google, find nearly nothing but the official example.

这篇 Dr.Dobb 的文章 The Boehm Collector for C and C++有一些很好的例子。

it seems like the destructor is never called.

引用文章:

but if you want the destructor to be called, you must delete the object yourself.

关于c++ - 如何在程序退出之前强制 boehm gc 全部收集,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16804666/

70 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com