在任意Linux系统,GCC 4.1.1或4.1.2环境下,建立以下工程:
Makefile
test: test.o agg.o
g++ -o test test.o agg.o
agg.o: agg.cpp agg.h base.h
g++ -g -c -Wall -o agg.o agg.cpp
test.o: test.cpp agg.h base.h
g++ -g -c -Wall -o test.o test.cpp
test.cpp
#include "agg.h"
int main(int, char**)
{
CAggretive obj;
return 0;
}
base.h
#include <stdio.h>
class CBase
{
public:
CBase() { printf("CBase/n"); }
~CBase() { printf("~CBase/n"); }
};
agg.h
#include <memory>
class CBase;
class CAggretive
{
public:
CAggretive();
~CAggretive() { printf("~CAggretive/n"); }
private:
std::auto_ptr<CBase> m_obj;
};
agg.cpp
#include "agg.h"
#include "base.h"
CAggretive::CAggretive() : m_obj(new CBase())
{
printf("CAggretive/n");
};
编译后得到目标可执行文件test。编译时得到警告信息:
g++ -g -c -Wall -o test.o test.cpp
/usr/lib/gcc/i386-redhat-linux/4.1.1/../../../../include/c++/4.1.1/memory: In destructor ‘std::auto_ptr<_Tp>::~auto_ptr() [with _Tp = CBase]’:
agg.h:9: instantiated from here
/usr/lib/gcc/i386-redhat-linux/4.1.1/../../../../include/c++/4.1.1/memory:259: warning: possible problem detected in invocation of delete operator:
/usr/lib/gcc/i386-redhat-linux/4.1.1/../../../../include/c++/4.1.1/memory:259: warning: invalid use of undefined type ‘struct CBase’
agg.h:3: warning: forward declaration of ‘struct CBase’
/usr/lib/gcc/i386-redhat-linux/4.1.1/../../../../include/c++/4.1.1/memory:259: note: neither the destructor nor the class-specific operator delete will be called, even if they are declared when the class is defined.
g++ -o test test.o agg.o
运行可执行文件test,程序输出:
CBase
CAggretive
~CAggretive
可以发现CBase的析构函数没有被调用,在实际应用中将造成内存和资源泄漏。
这是因为编译test.cpp时,包含文件树中没有base.h,所以编译器在实体化auto_ptr<CBase>类时不知道如何执行delete CBase型数据结构的方法。
在test.cpp最开头加上一句#include "base.h"即可解决此问题。编译agg.cpp时没有给出警告便是此原因。
(那么,为什么编译器知道如何new CBase型数据结构呢?那是因为CAggretive的构造函数是写在agg.cpp中,因此它的实现在agg.o中已经编译进去了)
因为像agg.h这样前置声明CBase,然后在类成员中使用它是一种不好的习惯,任何没有包含base.h而直接使用agg.h的cpp文件在编译时都会产生泄漏。
实际应用中可能会出现比本例更为复杂的引用情况,修改方式可能不那么简单。但总体思路是一样的,即是让编译器在编译此文件时确实包含用作auto_ptr的模板参数的类。