由于 C++ 语言没有自动内存回收机制,程序员每次 new 出来的内存都要手动 delete。程序员忘记 delete,,异常导致程序过早退出
int * p=new int; =>裸指针
- 必须手动释放
- 由于逻辑的错误,导致释放资源的代码没有被调用到
- 程序运行过程中,发生异常,导致释放资源的代码没有被调用到。
所以就需要智能指针:
不带引用计数的智能指针(资源无引用计数)
auto_ptr
scope_ptr
unique_ptr
带引用计数的智能指针(资源有引用计数)
shared_ptr 引用计数存在堆上
weak_ptr
强智能指针=》他可以改变资源的引用计数
带引用计数的强智能指针的缺陷:
循环/交叉引用的时候,造成资源无法释放
怎样解决(不对资源进行引用计数)弱智能指针
但他不可以直接访问对象
lock从弱智能指针 提升 强智能指针
shared_ptr<B>ptr=_ptrb.lock;
if(ptr!=NULL)
{
//表示提升成功,可以访问对象了
}
shared_ptr和weak_ptr都是线程安全的,可以在多线程环境下使用
把智能指针放在栈上,所有栈上的对象出了作用域会析构。
堆上的指针需要手动释放。
数据段上的程序结束释放。
CSmartPtr<int> ptr1(new int);
*ptr1=30;
CSmartPtr<int> ptr2(ptr1);
所有的智能指针都需要考虑浅拷贝问题
auto_ptr:不建议使用,因为会使原来的智能指针都失效了。
#include<iostream>
#include<memory>
#include<windows.h>
#include<unordered_map>
using namespace std;
/*专门用来统计资源的引用技术*/
class ReferenceCount
{
public:
//给ptr指向的资源增加引用计数
void incRef(void *ptr)
{
refMap[ptr]++;
/* unordered_map<void*, int>::iterator it = refMap.find(ptr);
if (it == refMap.end())
{
refMap.insert(make_pair(ptr, 1));
}
else
{
++(it->second);
}*/
}
//给ptr指向的资源减少引用计数
int decRef(void *ptr)
{
return refMap[ptr]--;
}
private:
//map的key表示资源地址, value表示资源的引用计数
unordered_map<void*, int> refMap;
};
template<typename T>
class CSmartPtr
{
public:
CSmartPtr(T *ptr = NULL)
:mptr(ptr)
{
if (mptr != NULL)
{
incRef();
}
}
CSmartPtr(const CSmartPtr<T> &src)
:mptr(src.mptr)
{
if (mptr != NULL)
{
incRef();
}
}
CSmartPtr<T>& operator=(const CSmartPtr<T> &src)
{
if (this == &src)
return *this;
if (decRef() == 0)
{
delete mptr;
mptr = NULL;
}
mptr = src.mptr;
if (mptr != NULL)
{
incRef();//多了一个指针指向当前内存
}
return *this;
}
~CSmartPtr()
{
if (decRef() == 0)
{
delete mptr;
mptr = NULL;
}
}
T& operator*()
{
return *mptr;
}
const T& operator*()const
{
return *mptr;
}
T* operator->()
{
return mptr;
}
T* operator->()const
{
return mptr;
}
private:
T *mptr;
//专门供智能指针,查看当前它所引用的资源的引用计数
static ReferenceCount refCnt;//所有指针共用一张表,设置为静态的
void incRef(){ refCnt.incRef(mptr); }
int decRef(){ return refCnt.decRef(mptr); }
};
template <typename T>
ReferenceCount CSmartPtr<T>::refCnt ;
class B;
class A
{
public:
A(){ cout << "A()" << endl;}
~A(){cout << "A()" << endl;}
//资源使用的地方,都使用弱智能指针
weak_ptr<B> _ptra;
};
class B
{
public:
B (){ cout << "B()" << endl;}
~B(){cout << "B()" << endl;}
//资源使用的地方,都使用弱智能指针
weak_ptr<A> _ptrb;
};
class Test
{
public:
Test(){ cout << "Test()" << endl; }
~Test(){ cout << "~Test()" << endl; }
void test(){ cout << "call Test::test" << endl; }
};
//线程参数的结构体类型
struct ThreadData
{
ThreadData(shared_ptr<Test> sp) :wp(sp){}
weak_ptr<Test> wp;
};
//子线程
DWORD WINAPI threadProc(void *lparg)
{
//::Sleep(2000);
ThreadData *p = (ThreadData*)lparg;
//p->test();
//通过weak智能指针的提升,来检测共享对象的存活状态
shared_ptr<Test> sp = p->wp.lock();
if (sp != NULL)
{
cout << "提升成功了!" << endl;
sp->test();
}
else
{
cout << "提升失败了" << endl;
}
delete p;
return 0;
}
void func()
{
//Test *p = new Test();
shared_ptr<Test> sp(new Test());
cout << sp.use_count() << endl;
ThreadData *data = new ThreadData(sp);
//创建一个线程 类似pthread_create
::CreateThread(NULL, 0, threadProc, data, 0, NULL);
//::Sleep(2000);
//delete p;
//::Sleep(5000);
}
int main()
{
func();
::Sleep(5000);
return 0;
}
int main(int argc, char* argv[])
{
//生成资源的时候采用强智能指针
shared_ptr<B>ptr1(new B());
shared_ptr<A>ptr2(new A());
//CSmartPtr<int> ptr1(new int);
//*ptr1=30;
//CSmartPtr<int> ptr2(ptr1);
//cout<<*ptr1<<endl;
const CSmartPtr<int> ptr2(new int(40));
cout<<*ptr2<<endl;
CSmartPtr<A> ptr3(new A());
(*ptr3).func();
ptr3->func();
const CSmartPtr<A> ptr4(new A());//常智能指针
ptr4->func();
return 0;
}