C++:delete不完整类型的指针

简介: 简单版 以下代码编译时会有warning: class X; void foo(X* x) { delete x; } 在GCC4.1.2下,编译出错信息是: warning: possible problem detected in invocation of delete oper.

简单版

以下代码编译时会有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的完整类型,没办法确定两件事情:

  1. X有没有自定义的析构函数(准确的说,有没有non-trivial的析构函数)。
  2. X有没有自定义的operator delete函数。

在不确定这两件事情的情况下,编译器只能按最普通的方式去处理delete x

  1. 不调用任何析构函数。
  2. 调用全局的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:

// test.h

include

class A {

class Impl;

public:

A();
~A();
void Func();

private:

std::auto_ptr<Impl> mImpl;

};

// test.cpp

include "test.h"

include

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
相关文章
|
7天前
|
C++
【C++11(三)】智能指针详解--RAII思想&循环引用问题
【C++11(三)】智能指针详解--RAII思想&循环引用问题
|
7天前
|
人工智能 C++
【重学C++】【指针】轻松理解常量指针和指针常量
【重学C++】【指针】轻松理解常量指针和指针常量
9 0
|
7天前
|
存储 人工智能 C++
【重学C++】【指针】详解让人迷茫的指针数组和数组指针
【重学C++】【指针】详解让人迷茫的指针数组和数组指针
27 1
|
7天前
|
存储 人工智能 编译器
【重学C++】【指针】一文看透:指针中容易混淆的四个概念、算数运算以及使用场景中容易忽视的细节
【重学C++】【指针】一文看透:指针中容易混淆的四个概念、算数运算以及使用场景中容易忽视的细节
18 1
|
7天前
|
C++
【C++】std::string 转换成非const类型 char* 的三种方法记录
【C++】std::string 转换成非const类型 char* 的三种方法记录
5 0
|
12天前
|
存储 安全 Java
Go语言学习10-指针类型
【4月更文挑战第11天】本篇 Huazie 向大家介绍 Go语言的指针类型
9 2
Go语言学习10-指针类型
|
15天前
|
存储 编译器 C++
【C++成长记】C++入门 | 类和对象(上) |类的作用域、类的实例化、类的对象大小的计算、类成员函数的this指针
【C++成长记】C++入门 | 类和对象(上) |类的作用域、类的实例化、类的对象大小的计算、类成员函数的this指针
|
19天前
|
存储 编译器 Linux
【C++初阶(十)】C++模板(进阶) ---非类型模板参数、模板的特化以及模板的分离编译
【C++初阶(十)】C++模板(进阶) ---非类型模板参数、模板的特化以及模板的分离编译
21 0
|
20天前
|
存储 C语言
C语言 — 指针进阶篇(下)
C语言 — 指针进阶篇(下)
20 0
|
20天前
|
存储 C语言 C++
C语言 — 指针进阶篇(上)
C语言 — 指针进阶篇(上)
27 0