【发布时间】:2018-03-24 16:20:04
【问题描述】:
下面的代码test-templated-destructor.cpp 复制了我正在使用的库的组织结构。我正在使用:
$ cat /etc/issue
Ubuntu 14.04.5 LTS \n \l
$ g++ --version
g++ (Ubuntu 4.8.4-2ubuntu1~14.04.3) 4.8.4
$ g++ -std=c++14
g++: error: unrecognized command line option ‘-std=c++14’
g++: fatal error: no input files
compilation terminated.
$ g++ -std=c++11
g++: fatal error: no input files
compilation terminated.
有:
- 基类
AA,以及从它派生的类BB和CC; - 抽象类
AAInstancer,以及从它派生的类AAInstancerTemplated模板化 - 类
AAHandler,它有一个模板化函数addTemplatedObject,它将AAInstancer*指向new AAInstancerTemplated<T>()对象的指针存储在类的map属性中 - 在
main()中,一个AAHandler对象被实例化,.addTemplatedObject<BB>("BB");调用它
如果我对此运行valgrind,它会报告:
==21000== 43 (16 direct, 27 indirect) bytes in 1 blocks are definitely lost in loss record 2 of 2
==21000== at 0x4C2B0E0: operator new(unsigned long) (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==21000== by 0x40141B: void AAHandler::addTemplatedObject<BB>(std::string) (test-templated-destructor.cpp:64)
==21000== by 0x40113E: main (test-templated-destructor.cpp:82)
我认为问题在于我们在addTemplatedObject() 中使用了new,因此我们应该在程序退出时相应地删除它——但这并没有完成,这就是泄漏的原因。
所以我想,写一个遍历instancers映射的迭代器,以及deletes这些指针在AAHandler的析构函数中,但我不能:
- 如果我写:
~AAHandler() {
cout << " (running AAHandler destructor)" << endl;
map<string, AAInstancer*>::iterator it;
for ( it = instancers.begin(); it != instancers.end(); it++ ) {
delete it->second;
}
}
...然后我开始编译:
$ g++ -g -Wall test-templated-destructor.cpp -o test-templated-destructor.exe
test-templated-destructor.cpp: In destructor ‘AAHandler::~AAHandler()’:
test-templated-destructor.cpp:60:18: warning: deleting object of abstract class type ‘AAInstancer’ which has non-virtual destructor will cause undefined behaviour [-Wdelete-non-virtual-dtor]
delete it->second;
^
... 听起来不错 - AAInstancer 没有定义析构函数,因此编译器可能自动添加为非虚拟的,导致此警告(尽管通过 valgrind 运行它会显示不再检测到泄漏)。
- 如果我写:
template <class T>
~AAHandler() {
cout << " (running AAHandler destructor)" << endl;
map<string, AAInstancer*>::iterator it;
for ( it = instancers.begin(); it != instancers.end(); it++ ) {
delete (AAInstancerTemplated<T>*)it->second;
}
}
...希望如果我们用一些模板调用addTemplatedObject(无论如何它不会),这个析构函数会被调用,编译失败:
$ g++ -g -Wall test-templated-destructor.cpp -o test-templated-destructor.exe && ./test-templated-destructor.exe
test-templated-destructor.cpp:57:14: error: destructor ‘AAHandler::~AAHandler()’ declared as member template
~AAHandler() {
^
...这也是有道理的:AAHandler 是一个非模板类,所以它的析构函数可能也不应该被模板化。
那么,是否可以为AAHandler 编写一个析构函数,它会将delete 的所有new 指针放在其instancers 中,无论它们是用哪个模板实例化的 - 使用最少(或最好,否)对现有代码的更改?
test-templated-destructor.cpp
// g++ -g -Wall test-templated-destructor.cpp -o test-templated-destructor.exe && ./test-templated-destructor.exe
// valgrind --leak-check=yes ./test-templated-destructor.exe
#include <iostream>
#include <map>
using namespace std;
class AA {
public:
string myname;
AA() {
myname = "";
cout << " AA instantiated\n";
}
};
class BB : public AA {
public:
string mystuff;
BB() {
mystuff = "";
cout << " BB instantiated\n";
}
};
class CC : public AA {
public:
string mythings;
CC() {
mythings = "";
cout << " CC instantiated\n";
}
};
class AAInstancer
{
public:
virtual AA* createInstance() = 0;
string tagName;
};
template <class T>
class AAInstancerTemplated: public AAInstancer
{
public:
AA* createInstance() {
return new T();
}
};
class AAHandler
{
public:
~AAHandler() { }
AAHandler() { }
static map<string, AAInstancer*> instancers;
template <class T>
static void addTemplatedObject(string tagName) {
AAInstancer* instancer = new AAInstancerTemplated<T>();
instancer->tagName = tagName;
instancers[tagName] = instancer;
}
AAHandler* get() {
if(singleton == NULL)
singleton = new AAHandler();
return singleton;
}
private:
static AAHandler* singleton;
};
map<string, AAInstancer*> AAHandler::instancers;
int main()
{
AAHandler aah;
aah.addTemplatedObject<BB>("BB");
cout << "Address of aah: " << static_cast<void*>(&aah) << endl;
return 0;
}
【问题讨论】:
-
为什么不用
std::unique_ptr<AAInstancer>而不是AAInstancer*? -
map<string, unique_ptr<AAInstancer>>怎么样,那你什么都不用做?