shared_ptr与weak_ptr

最近学习C++11,看到了强大的shared_ptr,心想,为何有了shared_ptr,还需要weak_ptr,weak_ptr是做什么的呢?经过探索,看到原因是shared_ptr内部也是基于引用计数的,引用计数的问题在于循环引用时,由于无法将计数值降为0而可能无法析构,示例如下:

View Code
#include "stdafx.h"

using namespace std;

struct Linker
{
    shared_ptr<Linker> link;
};

void DoWork()
{
    shared_ptr<Linker> l1(new Linker());
    shared_ptr<Linker> l2(new Linker());

    l1->link = l2;
    l2->link = l1;
}

int _tmain(int argc, _TCHAR* argv[])
{
    DoWork();
    _CrtDumpMemoryLeaks();
    return 0;
}

输出窗口内容如下:

View Code
Detected memory leaks!
Dumping objects ->
{196} normal block at 0x0060DD30, 16 bytes long.
 Data: <              ` > A4 D9 06 00 01 00 00 00 01 00 00 00 E8 DC 60 00 
{195} normal block at 0x0060DCE8, 8 bytes long.
 Data: <P `   ` > 50 DC 60 00 98 DC 60 00 
{194} normal block at 0x0060DC98, 16 bytes long.
 Data: <            P ` > A4 D9 06 00 01 00 00 00 01 00 00 00 50 DC 60 00 
{193} normal block at 0x0060DC50, 8 bytes long.
 Data: <  ` 0 ` > E8 DC 60 00 30 DD 60 00 
Object dump complete.

说明内存有泄露,解决办法很简单,只需要将Linker中的link成员类型从shared_ptr改成weak_ptr,作用是让该指针不引入计数:

View Code
#include "stdafx.h"

using namespace std;

struct Linker
{
    weak_ptr<Linker> link;
};

void DoWork()
{
    shared_ptr<Linker> l1(new Linker());
    shared_ptr<Linker> l2(new Linker());

    l1->link = l2;
    l2->link = l1;
}

int _tmain(int argc, _TCHAR* argv[])
{
    DoWork();
    _CrtDumpMemoryLeaks();
    return 0;
}

现在输出窗口平静了,什么也没有,说明没有内存泄漏!

你可能感兴趣的:(weak)