1.容器和智能指针是完美结合,内存究竟谁在释放?下面两篇文章解析
#include <iostream>
#include <list>
#include <memory>
using namespace std;
class Test
{
public:
Test(int i)
{
m_count = i;
cout << "Test()" << endl;
}
~Test()
{
cout << "~Test()" << endl;
}
int m_count;
};
void main()
{
#if 0
shared_ptr<list<shared_ptr<Test>>> plist = make_shared<list<shared_ptr<Test>>>();//局部变量,自动释放
shared_ptr<Test> pTest; //智能指针变量,指向最后一次赋值
cout<<"size of list ptr:"<<sizeof(list<shared_ptr<Test>>)<<"plist:"<<sizeof(plist)<<endl;
for(int i=0;i<10;i++)
{
pTest=make_shared<Test>(i);//创建对象赋值增加引用计数
plist->push_back(pTest); //赋值增加引用计数
}
auto itList= plist->begin();
while(itList!=plist->end())
{
if((*itList)->m_count == 5)
{
plist->erase(itList++); //会释放pTest对象,新标准智能指针会自动析构
}
else
{
cout<<"Loop List:"<<(*itList)->m_count<<endl;
itList++;
}
}
cout<<"*******************erase new using:***************************"<<endl;
itList= plist->begin();
while(itList!=plist->end())
{
if((*itList)->m_count == 6)
{
itList = plist->erase(itList); //1.会释放pTest对象,新标准智能指针会自动析构
} //2.新标准已经统一顺序容器和关联容器的erase,返回erase迭代子的下一个
else
{
cout<<"Loop List:"<<(*itList)->m_count<<endl;
itList++;
}
}
#endif
shared_ptr<Test> pTest;
list<shared_ptr<Test>> plist; //局部变量,自动释放
cout<<"size of plist "<<sizeof(plist)<<" shared_ptr Test: "<< sizeof(pTest)<<endl;
for(int i=0;i<10;i++)
{
pTest=make_shared<Test>(i);//创建对象赋值增加引用计数
plist.push_back(pTest); //赋值增加引用计数
}
auto itList= plist.begin();
while(itList!=plist.end())
{
if((*itList)->m_count == 5)
{
plist.erase(itList++); //会释放pTest对象,新标准智能指针会自动析构
}
else
{
cout<<"Loop List:"<<(*itList)->m_count<<endl;
itList++;
}
}
//cout<<"shared_ptr plist ref::"<<plist.use_count()<<endl;
cout<<"shared_ptr pTest ref::"<<pTest.use_count()<<endl;
}
*******************************************************************************************
1 Test::~Test main.cpp 19 0x13f7b5102
2 Test::`scalar deleting destructor' List 0x13f7b5c17
3 std::_Ref_count_obj<Test>::_Destroy memory 919 0x13f7b6257
4 std::_Ref_count_base::_Decref memory 113 0x13f7b6092
5 std::_Ptr_base<Test>::_Decref memory 339 0x13f7b6043
6 std::shared_ptr<Test>::~shared_ptr<Test> memory 573 0x13f7b50dc
7 std::shared_ptr<Test>::`scalar deleting destructor' List 0x13f7b5bc7
8 std::allocator<std::_List_node<std::shared_ptr<Test>,void * __ptr64>>::destroy<std::shared_ptr<Test>> xmemory0 665 0x13f7b401a
9 std::allocator_traits<std::allocator<std::_List_node<std::shared_ptr<Test>,void * __ptr64>>>::destroy<std::shared_ptr<Test>> xmemory0 783 0x13f7b404d
10 std::_Wrap_alloc<std::allocator<std::_List_node<std::shared_ptr<Test>,void * __ptr64>>>::destroy<std::shared_ptr<Test>> xmemory0 928 0x13f7b3fed
11 std::_List_buy<std::shared_ptr<Test>,std::allocator<std::shared_ptr<Test>>>::_Freenode list 852 0x13f7b6481
12 std::list<std::shared_ptr<Test>>::clear list 1509 0x13f7b72ce
13 std::list<std::shared_ptr<Test>>::_Tidy list 1886 0x13f7b6e63
14 std::list<std::shared_ptr<Test>>::~list<std::shared_ptr<Test>> list 1095 0x13f7b506c
15 std::list<std::shared_ptr<Test>>::`scalar deleting destructor' List 0x13f7b5b77
16 std::_Ref_count_obj<std::list<std::shared_ptr<Test>>>::_Destroy memory 919 0x13f7b6217
17 std::_Ref_count_base::_Decref memory 113 0x13f7b6092
18 std::_Ptr_base<std::list<std::shared_ptr<Test>>>::_Decref memory 339 0x13f7b6003
19 std::shared_ptr<std::list<std::shared_ptr<Test>>>::~shared_ptr<std::list<std::shared_ptr<Test>>> memory 573 0x13f7b50ac
20 main main.cpp 75 0x13f7b21f7
... <更多>
1 Test::~Test main.cpp 19 0x13f1a2fd2
2 Test::`scalar deleting destructor' List 0x13f1a5417
3 std::_Ref_count_obj<Test>::_Destroy memory 919 0x13f1a5ab7
4 std::_Ref_count_base::_Decref memory 113 0x13f1a5862
5 std::_Ptr_base<Test>::_Decref memory 339 0x13f1a3963
6 std::shared_ptr<Test>::~shared_ptr<Test> memory 573 0x13f1a4f2c
7 std::shared_ptr<Test>::`scalar deleting destructor' List 0x13f1a53c7
8 std::allocator<std::_List_node<std::shared_ptr<Test>,void * __ptr64>>::destroy<std::shared_ptr<Test>> xmemory0 665 0x13f1a3fba
9 std::allocator_traits<std::allocator<std::_List_node<std::shared_ptr<Test>,void * __ptr64>>>::destroy<std::shared_ptr<Test>> xmemory0 783 0x13f1a3fed
10 std::_Wrap_alloc<std::allocator<std::_List_node<std::shared_ptr<Test>,void * __ptr64>>>::destroy<std::shared_ptr<Test>> xmemory0 928 0x13f1a247d
11 std::_List_buy<std::shared_ptr<Test>,std::allocator<std::shared_ptr<Test>>>::_Freenode list 852 0x13f1a57b1
12 std::list<std::shared_ptr<Test>>::clear list 1509 0x13f1a5f9e
13 std::list<std::shared_ptr<Test>>::_Tidy list 1886 0x13f1a6783
14 std::list<std::shared_ptr<Test>>::~list<std::shared_ptr<Test>> list 1095 0x13f1a2a7c
15 main main.cpp 102 0x13f1a7323
... <更多>