C++:delete不完整类型的指针
简单版
以下代码编译时会有warning:
class X;
void foo(X* x) {
delete x;
}
在GCC4.1.2下,编译出错信息是:
warning: possible problem detected in invocation of delete operator:
warning: ‘x’ has incomplete type
warning: forward declaration of ‘struct X’
note: neither the destructor nor the class-specific operator delete will be called, even if they are declared when the class is defined.
这是因为在foo里,编译器看不到X的完整类型,没办法确定两件事情:
X有没有自定义的析构函数(准确的说,有没有non-trivial的析构函数)。X有没有自定义的operator delete函数。
在不确定这两件事情的情况下,编译器只能按最普通的方式去处理delete x:
- 不调用任何析构函数。
- 调用全局的
operator delete,通常来说就是直接释放内存。
日常版
有一个我们平常会遇到的场景,就会触发上面这个问题。
以下是由三个文件组成的一个工程,其中用到了'pImpl'方法来隐藏实现,因此在接口类中放了一个std::auto_ptr,很简单:
// test.h
#include <memory>
class A {
class Impl;
public:
A();
void Func();
private:
std::auto_ptr<Impl> mImpl;
};
// test.cpp
#include "test.h"
#include <iostream>
class A::Impl {
public:
void Func() {
std::cout << "Func" << std::endl;
}
};
A::A(): mImpl(new Impl) {}
void A::Func() {
mImpl->Func();
}
// main.cpp
#include "test.h"
int main() {
A a;
a.Func();
}
看起来很正常,但编译时有warning:
$g++ test.cpp main.cpp
In destructor ‘std::auto_ptr<_Tp>::~auto_ptr() [with _Tp = A::Impl]’:
test.h:4: instantiated from here
warning: possible problem detected in invocation of delete operator:
warning: invalid use of undefined type ‘struct A::Impl’
test.h:5: warning: forward declaration of ‘struct A::Impl’
note: neither the destructor nor the class-specific operator delete will be called, even if they are declared when the class is defined.
和前面说的warning信息完全一致,看起来也是在调用delete时出的问题。但哪里调用了delete呢?
答案是std::auto_ptr。
上面的代码中,我们没有给class A手动写一个析构函数,因为编译器自动生成的析构函数就是我们要的:析构时把mImpl析构掉。
那么自动生成的析构函数长什么样子呢?大概是:
A::~A() {
mImpl.~std::auto_ptr<Impl>();
}
展开了基本就是一句delete:
A::~A() {
delete mImpl._M_ptr;
}
这个析构函数的位置在哪呢?C++标准里说会把自动生成的类成员函数放在类的定义中,那么就是在test.h中。
问题清楚了:我们在编译main.cpp时,看不到A::Impl的完整定义,但却有一个自动生成的A::~A,其中delete了一个不完整的类对象!
解法
手动写一个A的析构函数,位置要在能看到A::Impl完整定义的地方,也就是test.cpp:
```cpp
// test.h
#include <memory>
class A {
class Impl;
public:
A();
~A();
void Func();
private:
std::auto_ptr<Impl> mImpl;
};
// test.cpp
#include "test.h"
#include <iostream>
class A::Impl {
public:
void Func() {
std::cout << "Func" << std::endl;
}
};
A::A(): mImpl(new Impl) {}
A::~A() {}
void A::Func() {
mImpl->Func();
}
相关文献
- http://stackoverflow.com/questions/4325154/delete-objects-of-incomplete-type
- http://stackoverflow.com/questions/4023794/forward-declaration-just-wont-do
C++:delete不完整类型的指针的更多相关文章
- C语言的不完整类型和前置声明(转)
声明与定义(Declaration and Definition) 开始这篇文章之前,我们先弄懂变量的declaration和definition的区别,即变量的声明和定义的区别. 一般情况下,我们这 ...
- 为什么C++类定义中,数据成员不能被指定为自身类型,但可以是指向自身类型的指针或引用?为什么在类体内可以定义将静态成员声明为其所属类的类型呢 ?
static的成员变量,不是存储在Bar实例之中的,因而不会有递归定义的问题. 类声明: class Screen: //Screen类的声明 1 类定义: class Screen{ //Scree ...
- 从默认析构函数学习c++,new,delete,内存泄漏,野指针
默认析构函数:当系统没有显式定义析构函数,编译器同样会为对象定义一个默认析构函数,默认的析构函数只能释放普通数据成员所占用的空间,无法通过释放通过new和malloc进行申请的空间,因此避免内存泄漏, ...
- 不同类型的指针+1之后增加的大小不同(a,&a的地址是一样的,但意思不一样)
main() { ]={,,,,}; ); printf(),*(ptr-)); } *(a+1)就是a[1],*(ptr-1)就是a[4], 执行结果是2, 5.&a+1不是首地址+1,系统 ...
- C++:不同类型的指针的本质与差异
转自:http://blog.csdn.net/richerg85/article/details/10076365 指针的类型(The Type of a Pointer) 一 ...
- 直接修改托管堆栈中的type object pointer(类型对象指针)
都知道.NET是一个强对象类型的框架. 那么对于对象类型又是怎么确定的呢. 最初的我简单认为数据的类型就是定义时字段的类型修饰决定的(回来发现这种观点是绝对错误的) 我们知道引用对象存储在托管堆栈中, ...
- C# CLR via 对象内存中堆的存储【类型对象指针、同步块索引】
最近在看书,看到了对象在内存中的存储方式. 讲到了对象存储在内存堆中,分配的空间除了类型对象的成员所需的内存量,还有额外的成员(类型对象指针. 同步块索引 ),看到这个我就有点不懂了,不知道类型对象指 ...
- LPVOID 没有类型的指针
可以将LPVOID类型的变量赋值给任意类型的指针,比如在参数传递时就可以把任意类型传递给一个LPVOID类型为参数的方法,然后在方法内再将这个“任意类型”从传递时的“LPVOID类型”转换回来. 示例 ...
- python_递归实现汉诺塔 (string类型的指针出错 未解决)
在递归的时候,和数学的归纳法一致. void func( mode) { if(endCondition) { constExpression //基本项 } else { accumrateExpr ...
随机推荐
- 人活着系列之开会(Floy)
http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=2930 题意:所有点到Z点的最短距离.因为岛名由 ...
- 最小生成树(kruskal模版 Prim模板)
http://acm.sdut.edu.cn/sdutoj/showproblem.php?pid=2144&cid=1186 最小生成树,最重要的是了解思想 稠密图用Prim,稀疏图用Kru ...
- SpringMyBatisDay01
1.Spring简介 Spring是一个开源轻量级应用开发框架,其目的是用于简化企业级应用程序的开发,降低侵入性Spring提供IOC和AOP功能,可以将组件(就是类)之间的耦合度降至最低,解耦,便于 ...
- Object-C-NSFileManager
+(NSFileManager *)defaultManager;//获得文件管理对象 -(BOOL)createFileAtPath:(NSString *)path contents:(NSDat ...
- 10588 - Queuing at the doctors
这题wa 了 八次 你说 巨弱 orz 大神 总结一下 没有将所有的情况考虑清楚 ,当有的时候一个人已经全部看完的时候 别的人还没开始 但是我就把他给结束了 #include <iost ...
- MySQL从删库到跑路_高级(七)——事务和锁
作者:天山老妖S 链接:http://blog.51cto.com/9291927 一.事务简介 1.事务简介 事务(Transaction)是指作为单个逻辑工作单元执行的一系列操作. 2.事物的特效 ...
- [備註] 安裝與整合 xUnit 測試框架@VS2012
開發環境:Visual Studio 2012, Update 1 (必須). 說明:VS2012 已整合各測試框架的 Test Runner,包括 NUnit, xUnit 等.藉由標準的介面,可讓 ...
- python环境安装selenium和手动下载安装selenium的方法
方法1:cmd环境下,用pip install selenium 可能会很慢 方法2:下载selenium安装包手动安装 下载地址:https://pypi.org/project/selenium/ ...
- iperf3.0 hisi uclib 交叉编译
1. 下载iperf src https://github.com/esnet/iperf/ 2.修改makefile.in 里面的配置. src/Makefile.in 613行 地方两行,去掉-p ...
- CSS style 属性
CSS style 属性 定义和用法 必需的 type 属性规定样式表的 MIME 类型. type 属性指示 <style> 与 </style> 标签之间的内容. 值 &q ...