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 ...
随机推荐
- [py][mx]django-解决注册用户已存在,激活链接判断
注册时候,如果用户已存在,则提示错误 激活用户时候,如果激活链接失效,则提示用户. class RegisterView(View): def get(self, request): register ...
- hdu1505City Game(扫描线)
http://acm.hdu.edu.cn/showproblem.php?pid=1505 题意:R为被占位置,F为空位,求出最大子空矩阵大小*3. 思路:1.悬线法,记录每个位置的悬线能到达的左边 ...
- c#实现图片二值化例子(黑白效果)
C#将图片2值化示例代码,原图及二值化后的图片如下: 原图: 二值化后的图像: 实现代码: ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 2 ...
- Quick Look at the Air Jordan 32
A color with 25 years of history in the Air Jordan line will once again leave its mark on the Air Jo ...
- 7.11 Models -- Customizing Adapters
一.概述 1. 在Ember Data中,和后台数据存储通信的逻辑存在于Adapter中.Ember Data的有一些内置的假设,一个 REST API 应该怎么看.如果你的后台约定和这些假设不同,E ...
- 9/24matplotlib使用入门
---恢复内容开始--- matplotlib的使用中有好几种输出风格,有matlab风格,和官方文档的as风格,各有所长,本文对比介绍官方文档中的使用风格. 我们画图的目的是要将函数以图像显示出来, ...
- transform 和 transition
transform的属性包括:rotate() / skew() / scale() / translate() /matrix() 其中 rotate() 旋转度数,0-360 skew() 元素 ...
- hdu3511 Prison Break 圆的扫描线
地址:http://acm.split.hdu.edu.cn/showproblem.php?pid=3511 题目: Prison Break Time Limit: 10000/5000 MS ( ...
- python 类的私有方法例子
#coding=utf-8 class Person(object): id=12 def __init__(self,name): self.name=name ...
- 页面点击,不是a标签也会刷新原因
页面点击,不是a标签也会刷新原因 点击事件冒泡,触发了a链接导致整个页面刷新了.直接阻止 事件冒泡即可 例子: $("tr .am-text-danger").click(func ...