关于GCC编译程序报出警告:note: neither the destructor nor the class-specific operator delete will be called...的问题及解决方案

在任意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的模板参数的类。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值