C++ 11新特性学习笔记- 智能指针

很多人谈到c++,说它特别难,可能有一部分就是因为c++的内存管理吧,不像java那样有虚拟机动态的管理内存,在程序运行过程中可能就会出现内存泄漏,然而这种问题其实都可以通过c++11引入的智能指针来解决,相反我还认为这种内存管理还是c++语言的优势,因为尽在掌握。

c++11引入了三种智能指针:

[TOC]

shared_ptr

shared_ptr使用了引用计数,每一个shared_ptr的拷贝都指向相同的内存,每次拷贝都会触发引用计数+1,每次生命周期结束析构的时候引用计数-1,在最后一个shared_ptr析构的时候,内存才会释放。

使用方法如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
struct ClassWrapper {

ClassWrapper() {
cout << "construct" << endl;
data = new int[10];
}

~ClassWrapper() {
cout << "deconstruct" << endl;
if (data != nullptr) {
delete[] data;
}
}

void Print() {
cout << "print" << endl;
}

int* data;
};

void Func(std::shared_ptr<ClassWrapper> ptr) {
ptr->Print();
}

int main() {
auto smart_ptr = std::make_shared<ClassWrapper>();
auto ptr2 = smart_ptr; // 引用计数+1
ptr2->Print();
Func(smart_ptr); // 引用计数+1
smart_ptr->Print();
ClassWrapper *p = smart_ptr.get(); // 可以通过get获取裸指针
p->Print();
return 0;
}

智能指针还可以自定义删除器,在引用计数为0的时候自动调用删除器来释放对象的内存,代码如下:

1
std::shared_ptr<int> ptr(new int, [](int *p){ delete p; });

关于shared_ptr有几点需要注意:

  • 不要用一个裸指针初始化多个shared_ptr,会出现double_free导致程序崩溃

  • 通过shared_from_this()返回this指针,不要把this指针作为shared_ptr返回出来,因为this指针本质就是裸指针,通过this返回可能 会导致重复析构,不能把this指针交给智能指针管理。

    1
    2
    3
    4
    5
    6
    class A {
    shared_ptr<A> GetSelf() {
    return shared_from_this();
    // return shared_ptr<A>(this); 错误,会导致double free
    }
    };
  • 尽量使用make_shared,少用new。

  • 不要delete get()返回来的裸指针。

  • 不是new出来的空间要自定义删除器。

  • 要避免循环引用,循环引用导致内存永远不会被释放,造成内存泄漏。

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    using namespace std;
    struct A;
    struct B;

    struct A {
    std::shared_ptr<B> bptr;
    ~A() {
    cout << "A delete" << endl;
    }
    };

    struct B {
    std::shared_ptr<A> aptr;
    ~B() {
    cout << "B delete" << endl;
    }
    };

    int main() {
    auto aaptr = std::make_shared<A>();
    auto bbptr = std::make_shared<B>();
    aaptr->bptr = bbptr;
    bbptr->aptr = aaptr;
    return 0;
    }

    上面代码,产生了循环引用,导致aptr和bptr的引用计数为2,离开作用域后aptr和bptr的引用计数-1,但是永远不会为0,导致指针永远不会析构,产生了内存泄漏,如何解决这种问题呢,答案是使用weak_ptr

weak_ptr

weak_ptr是用来监视shared_ptr的生命周期,它不管理shared_ptr内部的指针,它的拷贝的析构都不会影响引用计数,纯粹是作为一个旁观者监视shared_ptr中管理的资源是否存在,可以用来返回this指针和解决循环引用问题。

  • 作用1:返回this指针,上面介绍的shared_from_this()其实就是通过weak_ptr返回的this指针,这里参考我之前写的源码分析shared_ptr实现的文章,最后附上链接。

  • 作用2:解决循环引用问题。

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    struct A;
    struct B;

    struct A {
    std::shared_ptr<B> bptr;
    ~A() {
    cout << "A delete" << endl;
    }
    void Print() {
    cout << "A" << endl;
    }
    };

    struct B {
    std::weak_ptr<A> aptr; // 这里改成weak_ptr
    ~B() {
    cout << "B delete" << endl;
    }
    void PrintA() {
    if (!aptr.expired()) { // 监视shared_ptr的生命周期
    auto ptr = aptr.lock();
    ptr->Print();
    }
    }
    };

    int main() {
    auto aaptr = std::make_shared<A>();
    auto bbptr = std::make_shared<B>();
    aaptr->bptr = bbptr;
    bbptr->aptr = aaptr;
    bbptr->PrintA();
    return 0;
    }
    输出:
    A
    A delete
    B delete

unique_ptr

std::unique_ptr是一个独占型的智能指针,它不允许其它智能指针共享其内部指针,也不允许unique_ptr的拷贝和赋值。使用方法和shared_ptr类似,区别是不可以拷贝:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
using namespace std;

struct A {
~A() {
cout << "A delete" << endl;
}
void Print() {
cout << "A" << endl;
}
};


int main() {
auto ptr = std::unique_ptr<A>(new A);
auto tptr = std::make_unique<A>(); // error, c++11还不行,需要c++14
std::unique_ptr<A> tem = ptr; // error, unique_ptr不允许移动
ptr->Print();
return 0;
}

unique_ptr也可以像shared_ptr一样自定义删除器,使用方法和shared_ptr相同。


C++ 11新特性学习笔记- 智能指针
http://pla.com/2022/03/19/C11/智能指针/
作者
RenMingchang
发布于
2022年3月19日
许可协议