C++ 在CCC和X2B的中间抛出GCC(但不是Clang)时的内存泄漏;x2B;14标准的初始值设定项列表::列表<;共享\u ptr>;
考虑以下计划:C++ 在CCC和X2B的中间抛出GCC(但不是Clang)时的内存泄漏;x2B;14标准的初始值设定项列表::列表<;共享\u ptr>;,c++,exception-handling,c++14,shared-ptr,initializer-list,C++,Exception Handling,C++14,Shared Ptr,Initializer List,考虑以下计划: #include <stdexcept> #include <stdio.h> #include <memory> #include <list> class Foo { public: Foo(){ if (s_ct==0) {throw std::bad_alloc();} --s_ct; fprintf(stderr, "ctor %p\n", this); }
#include <stdexcept>
#include <stdio.h>
#include <memory>
#include <list>
class Foo {
public:
Foo(){
if (s_ct==0) {throw std::bad_alloc();}
--s_ct;
fprintf(stderr, "ctor %p\n", this);
}
~Foo(){
fprintf(stderr, "dtor %p\n", this);
}
private:
static int s_ct;
};
int Foo::s_ct = 2;
int main(){
try {
std::list<std::shared_ptr<Foo>> l = {
std::make_shared<Foo>(),
std::make_shared<Foo>(),
std::make_shared<Foo>()
};
} catch (std::bad_alloc&) {
fprintf(stderr, "caught exception.\n");
}
fprintf(stderr, "done.\n");
return 0;
}
输出为:
[little:~] $ ./list_init
ctor 0x1294c30
ctor 0x1294c50
caught exception.
done.
[little:~] $
请注意,没有调用析构函数。Valgrind也正确地抱怨泄漏
这似乎违反了std::make_shared
的一个关键目的——即,如果语句中的另一个表达式抛出,共享对象将被正确销毁,因为它由完全构造的共享指针对象包装
Clang做了我想做的事情:
[little:~] $ clang++ --version
clang version 3.8.0-2ubuntu4 (tags/RELEASE_380/final)
Target: x86_64-pc-linux-gnu
Thread model: posix
InstalledDir: /usr/bin
[little:~] $ clang++ --std=c++14 -o foo list_init.cc
[little:~] $ ./foo
ctor 0x1dfec30
ctor 0x1dfec50
dtor 0x1dfec50
dtor 0x1dfec30
caught exception.
done.
[little:~] $
那么,这是一个GCC错误,还是我需要修复我的程序?将我的评论转换为答案,这样您就可以将问题标记为已回答
这看起来是一个gcc错误
-未为部分构造的匿名结构/数组的成员调用析构函数
请特别注意最后两个使用
std::initializer\u list
来说明问题的测试用例。看起来像一个“未为部分构造的匿名结构/数组的成员调用析构函数”。它与部分对象(在构造函数引发异常时创建)有什么不同吗以后不会调用析构函数
[little:~] $ clang++ --version
clang version 3.8.0-2ubuntu4 (tags/RELEASE_380/final)
Target: x86_64-pc-linux-gnu
Thread model: posix
InstalledDir: /usr/bin
[little:~] $ clang++ --std=c++14 -o foo list_init.cc
[little:~] $ ./foo
ctor 0x1dfec30
ctor 0x1dfec50
dtor 0x1dfec50
dtor 0x1dfec30
caught exception.
done.
[little:~] $