关于GCC编译程序报出警告:note: neither the destructor nor the class-specific operator delete will be called...的问题及解决方案
2010-08-12 14:35
573 查看
在任意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的模板参数的类。
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的模板参数的类。
相关文章推荐
- 关于Classpath entry /XX/xx.jar will not be exported or published.警告的消除
- java程序运行时出现问题:Could not find the main class: test_jena. Program will exit.
- 出现“OnDestroy or PostNcDestroy in derived class will not be called.”问题
- Eclipse下编译出现错误:can not find the main class,Program will exit 问题解决
- maven -- 问题解决(四)警告Classpath entry org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER will not be exported or published
- arm环境下编译出现下面警告的解决办法 note: the mangling of 'va_list' has changed in GCC 4.4
- 最新自动化编译脚本,关于gradle编译遇到的一些问题的解决方案
- 一个关于VS2008在其他未安装VS2008的电脑上无法运行编译的程序的问题 收藏
- linux编译警告 will be initialized after
- 警告: A docBase inside the host appBase has been specified, and will be ignore2
- Class JavaLaunchHelper is implemented in both … libinstrument.dylib. One of the two will be used. Wh
- 关于问题The fully qualified name of the bean's class, except if it serves only as a parent definition fo
- gcc编译静态库到自己的程序 解决在不同linux下因libc版本问题而不能运行
- 关于Python的第三方库requests关闭SSL验证后,依然警告致使程序无法执行问题
- Linux里面的gcc编译程序游离问题
- Class JavaLaunchHelper is implemented in both … libinstrument.dylib. One of the two will be used. Wh
- Linux遇到的问题(一)Ubuntu报“xxx is not in the sudoers file.This incident will be reported” 错误解决方法
- ant中com.sun.tools.javac.Main is not on the classpath问题的解决方案
- Linux系统平台下关于GCC编译及使用的方法(-Werror,它要求GCC将所有的警告当成错误进行处理 )
- 关于Eclipse 中用Ant编译Java程序的JDK环境设置问题