使用 shared_ptr 的一個問題代碼
2015-09-24 07:43
363 查看
代碼如下:
#include <iostream>
#include <memory>
#include <list>
class A
{
public:
void Add();
void Delete();
~A();
private:
std::list< std::shared_ptr<A> > m_ptr;
};
void A::Add(void)
{
m_ptr.push_back(std::shared_ptr<A> (this));
}
void A::Delete(void)
{
m_ptr.remove(std::shared_ptr<A> (this));
}
A::~A()
{
std::cout << "A destructor " << std::endl;
}
int main(void)
{
A * pA = new A();
pA->Add();
pA->Delete();
return 0;
}
運行的時候crash.
死機的時候的backtrace如下:
(gdb) bt full
#0 0x00011550 in std::_List_base<std::shared_ptr<A>, std::allocator<std::shared_ptr<A> > >::_M_clear (this=0x23008)
at /toolchain/arm-linux-4.8.2/arm-linux-gnueabi/include/c++/4.8.2/bits/list.tcc:73
__tmp = 0x0
__cur = 0x0
#1 0x00011130 in std::_List_base<std::shared_ptr<A>, std::allocator<std::shared_ptr<A> > >::~_List_base (this=0x23008, __in_chrg=<optimized out>)
at /toolchain/arm-linux-4.8.2/arm-linux-gnueabi/include/c++/4.8.2/bits/stl_list.h:378
No locals.
#2 0x00010e9c in std::list<std::shared_ptr<A>, std::allocator<std::shared_ptr<A> > >::~list (this=0x23008, __in_chrg=<optimized out>)
at /toolchain/arm-linux-4.8.2/arm-linux-gnueabi/include/c++/4.8.2/bits/stl_list.h:438
No locals.
#3 0x00010d0c in A::~A (this=0x23008, __in_chrg=<optimized out>)
at test_delete.cpp:29
No locals.
#4 0x00011db4 in std::_Sp_counted_ptr<A*, (__gnu_cxx::_Lock_policy)2>::_M_dispose (this=0x23048)
at /toolchain/arm-linux-4.8.2/arm-linux-gnueabi/include/c++/4.8.2/bits/shared_ptr_base.h:290
---Type <return> to continue, or q <return> to quit---
No locals.
#5 0x0001122c in std::_Sp_counted_base<(__gnu_cxx::_Lock_policy)2>::_M_release (this=0x23048)
at /toolchain/arm-linux-4.8.2/arm-linux-gnueabi/include/c++/4.8.2/bits/shared_ptr_base.h:144
No locals.
#6 0x00010f3c in std::__shared_count<(__gnu_cxx::_Lock_policy)2>::~__shared_count (this=0xbefffcb4, __in_chrg=<optimized out>)
at /toolchain/arm-linux-4.8.2/arm-linux-gnueabi/include/c++/4.8.2/bits/shared_ptr_base.h:546
No locals.
#7 0x00010e44 in std::__shared_ptr<A, (__gnu_cxx::_Lock_policy)2>::~__shared_ptr (this=0xbefffcb0, __in_chrg=<optimized out>)
at /toolchain/arm-linux-4.8.2/arm-linux-gnueabi/include/c++/4.8.2/bits/shared_ptr_base.h:781
No locals.
#8 0x00010e70 in std::shared_ptr<A>::~shared_ptr (this=0xbefffcb0,
__in_chrg=<optimized out>)
at /toolchain/arm-linux-4.8.2/arm-linux-gnueabi/include/c++/4.8.2/bits/shared_ptr.h:93
No locals.
#9 0x00010cac in A::Delete (this=0x23008) at test_delete.cpp:23
No locals.
---Type <return> to continue, or q <return> to quit---
#10 0x00010d64 in main () at test_delete.cpp:35
pA = 0x23008
从 log看,class A的析构函数被调用了两次。这可能是问题的原因。
第一次调用A的析构函数的backtrace:
(gdb) where
#0 A::~A (this=0x23008, __in_chrg=<optimized out>) at test_delete.cpp:28
#1 0x00011db4 in std::_Sp_counted_ptr<A*, (__gnu_cxx::_Lock_policy)2>::_M_dispose (this=0x23018)
at /toolchain/arm-linux-4.8.2/arm-linux-gnueabi/include/c++/4.8.2/bits/shared_ptr_base.h:290
#2 0x0001122c in std::_Sp_counted_base<(__gnu_cxx::_Lock_policy)2>::_M_release (this=0x23018)
at /toolchain/arm-linux-4.8.2/arm-linux-gnueabi/include/c++/4.8.2/bits/shared_ptr_base.h:144
#3 0x00010f3c in std::__shared_count<(__gnu_cxx::_Lock_policy)2>::~__shared_count (this=0x2303c, __in_chrg=<optimized out>)
at /toolchain/arm-linux-4.8.2/arm-linux-gnueabi/include/c++/4.8.2/bits/shared_ptr_base.h:546
#4 0x00010e44 in std::__shared_ptr<A, (__gnu_cxx::_Lock_policy)2>::~__shared_ptr (this=0x23038, __in_chrg=<optimized out>)
at /toolchain/arm-linux-4.8.2/arm-linux-gnueabi/include/c++/4.8.2/bits/shared_ptr_base.h:781
#5 0x00010e70 in std::shared_ptr<A>::~shared_ptr (this=0x23038,
__in_chrg=<optimized out>)
at /toolchain/arm-linux-4.8.2/arm-linux-gnueabi/include/c++/4.8.2/bits/shared_ptr.h:93
#6 0x000117d8 in std::_List_node<std::shared_ptr<A> >::~_List_node (
this=0x23030, __in_chrg=<optimized out>)
---Type <return> to continue, or q <return> to quit---
at /toolchain/arm-linux-4.8.2/arm-linux-gnueabi/include/c++/4.8.2/bits/stl_list.h:106
#7 0x00011804 in __gnu_cxx::new_allocator<std::_List_node<std::shared_ptr<A> > >::destroy<std::_List_node<std::shared_ptr<A> > > (this=0x23008, __p=0x23030)
at /toolchain/arm-linux-4.8.2/arm-linux-gnueabi/include/c++/4.8.2/ext/new_allocator.h:124
#8 0x000114e4 in std::list<std::shared_ptr<A>, std::allocator<std::shared_ptr<A> > >::_M_erase (this=0x23008, __position=...)
at /toolchain/arm-linux-4.8.2/arm-linux-gnueabi/include/c++/4.8.2/bits/stl_list.h:1573
#9 0x0001107c in std::list<std::shared_ptr<A>, std::allocator<std::shared_ptr<A> > >::remove (this=0x23008, __value=...)
at /toolchain/arm-linux-4.8.2/arm-linux-gnueabi/include/c++/4.8.2/bits/list.tcc:254
#10 0x00010ca0 in A::Delete (this=0x23008) at test_delete.cpp:23
#11 0x00010d64 in main () at test_delete.cpp:35
第二次:
(gdb) where
#0 A::~A (this=0x23008, __in_chrg=<optimized out>) at test_delete.cpp:28
#1 0x00011db4 in std::_Sp_counted_ptr<A*, (__gnu_cxx::_Lock_policy)2>::_M_dispose (this=0x23048)
at /toolchain/arm-linux-4.8.2/arm-linux-gnueabi/include/c++/4.8.2/bits/shared_ptr_base.h:290
#2 0x0001122c in std::_Sp_counted_base<(__gnu_cxx::_Lock_policy)2>::_M_release (this=0x23048)
at /toolchain/arm-linux-4.8.2/arm-linux-gnueabi/include/c++/4.8.2/bits/shared_ptr_base.h:144
#3 0x00010f3c in std::__shared_count<(__gnu_cxx::_Lock_policy)2>::~__shared_count (this=0xbefffcb4, __in_chrg=<optimized out>)
at /toolchain/arm-linux-4.8.2/arm-linux-gnueabi/include/c++/4.8.2/bits/shared_ptr_base.h:546
#4 0x00010e44 in std::__shared_ptr<A, (__gnu_cxx::_Lock_policy)2>::~__shared_ptr (this=0xbefffcb0, __in_chrg=<optimized out>)
at /toolchain/arm-linux-4.8.2/arm-linux-gnueabi/include/c++/4.8.2/bits/shared_ptr_base.h:781
#5 0x00010e70 in std::shared_ptr<A>::~shared_ptr (this=0xbefffcb0,
__in_chrg=<optimized out>)
at /toolchain/arm-linux-4.8.2/arm-linux-gnueabi/include/c++/4.8.2/bits/shared_ptr.h:93
#6 0x00010cac in A::Delete (this=0x23008) at test_delete.cpp:23
#7 0x00010d64 in main () at test_delete.cpp:35
#include <iostream>
#include <memory>
#include <list>
class A
{
public:
void Add();
void Delete();
~A();
private:
std::list< std::shared_ptr<A> > m_ptr;
};
void A::Add(void)
{
m_ptr.push_back(std::shared_ptr<A> (this));
}
void A::Delete(void)
{
m_ptr.remove(std::shared_ptr<A> (this));
}
A::~A()
{
std::cout << "A destructor " << std::endl;
}
int main(void)
{
A * pA = new A();
pA->Add();
pA->Delete();
return 0;
}
運行的時候crash.
死機的時候的backtrace如下:
(gdb) bt full
#0 0x00011550 in std::_List_base<std::shared_ptr<A>, std::allocator<std::shared_ptr<A> > >::_M_clear (this=0x23008)
at /toolchain/arm-linux-4.8.2/arm-linux-gnueabi/include/c++/4.8.2/bits/list.tcc:73
__tmp = 0x0
__cur = 0x0
#1 0x00011130 in std::_List_base<std::shared_ptr<A>, std::allocator<std::shared_ptr<A> > >::~_List_base (this=0x23008, __in_chrg=<optimized out>)
at /toolchain/arm-linux-4.8.2/arm-linux-gnueabi/include/c++/4.8.2/bits/stl_list.h:378
No locals.
#2 0x00010e9c in std::list<std::shared_ptr<A>, std::allocator<std::shared_ptr<A> > >::~list (this=0x23008, __in_chrg=<optimized out>)
at /toolchain/arm-linux-4.8.2/arm-linux-gnueabi/include/c++/4.8.2/bits/stl_list.h:438
No locals.
#3 0x00010d0c in A::~A (this=0x23008, __in_chrg=<optimized out>)
at test_delete.cpp:29
No locals.
#4 0x00011db4 in std::_Sp_counted_ptr<A*, (__gnu_cxx::_Lock_policy)2>::_M_dispose (this=0x23048)
at /toolchain/arm-linux-4.8.2/arm-linux-gnueabi/include/c++/4.8.2/bits/shared_ptr_base.h:290
---Type <return> to continue, or q <return> to quit---
No locals.
#5 0x0001122c in std::_Sp_counted_base<(__gnu_cxx::_Lock_policy)2>::_M_release (this=0x23048)
at /toolchain/arm-linux-4.8.2/arm-linux-gnueabi/include/c++/4.8.2/bits/shared_ptr_base.h:144
No locals.
#6 0x00010f3c in std::__shared_count<(__gnu_cxx::_Lock_policy)2>::~__shared_count (this=0xbefffcb4, __in_chrg=<optimized out>)
at /toolchain/arm-linux-4.8.2/arm-linux-gnueabi/include/c++/4.8.2/bits/shared_ptr_base.h:546
No locals.
#7 0x00010e44 in std::__shared_ptr<A, (__gnu_cxx::_Lock_policy)2>::~__shared_ptr (this=0xbefffcb0, __in_chrg=<optimized out>)
at /toolchain/arm-linux-4.8.2/arm-linux-gnueabi/include/c++/4.8.2/bits/shared_ptr_base.h:781
No locals.
#8 0x00010e70 in std::shared_ptr<A>::~shared_ptr (this=0xbefffcb0,
__in_chrg=<optimized out>)
at /toolchain/arm-linux-4.8.2/arm-linux-gnueabi/include/c++/4.8.2/bits/shared_ptr.h:93
No locals.
#9 0x00010cac in A::Delete (this=0x23008) at test_delete.cpp:23
No locals.
---Type <return> to continue, or q <return> to quit---
#10 0x00010d64 in main () at test_delete.cpp:35
pA = 0x23008
从 log看,class A的析构函数被调用了两次。这可能是问题的原因。
第一次调用A的析构函数的backtrace:
(gdb) where
#0 A::~A (this=0x23008, __in_chrg=<optimized out>) at test_delete.cpp:28
#1 0x00011db4 in std::_Sp_counted_ptr<A*, (__gnu_cxx::_Lock_policy)2>::_M_dispose (this=0x23018)
at /toolchain/arm-linux-4.8.2/arm-linux-gnueabi/include/c++/4.8.2/bits/shared_ptr_base.h:290
#2 0x0001122c in std::_Sp_counted_base<(__gnu_cxx::_Lock_policy)2>::_M_release (this=0x23018)
at /toolchain/arm-linux-4.8.2/arm-linux-gnueabi/include/c++/4.8.2/bits/shared_ptr_base.h:144
#3 0x00010f3c in std::__shared_count<(__gnu_cxx::_Lock_policy)2>::~__shared_count (this=0x2303c, __in_chrg=<optimized out>)
at /toolchain/arm-linux-4.8.2/arm-linux-gnueabi/include/c++/4.8.2/bits/shared_ptr_base.h:546
#4 0x00010e44 in std::__shared_ptr<A, (__gnu_cxx::_Lock_policy)2>::~__shared_ptr (this=0x23038, __in_chrg=<optimized out>)
at /toolchain/arm-linux-4.8.2/arm-linux-gnueabi/include/c++/4.8.2/bits/shared_ptr_base.h:781
#5 0x00010e70 in std::shared_ptr<A>::~shared_ptr (this=0x23038,
__in_chrg=<optimized out>)
at /toolchain/arm-linux-4.8.2/arm-linux-gnueabi/include/c++/4.8.2/bits/shared_ptr.h:93
#6 0x000117d8 in std::_List_node<std::shared_ptr<A> >::~_List_node (
this=0x23030, __in_chrg=<optimized out>)
---Type <return> to continue, or q <return> to quit---
at /toolchain/arm-linux-4.8.2/arm-linux-gnueabi/include/c++/4.8.2/bits/stl_list.h:106
#7 0x00011804 in __gnu_cxx::new_allocator<std::_List_node<std::shared_ptr<A> > >::destroy<std::_List_node<std::shared_ptr<A> > > (this=0x23008, __p=0x23030)
at /toolchain/arm-linux-4.8.2/arm-linux-gnueabi/include/c++/4.8.2/ext/new_allocator.h:124
#8 0x000114e4 in std::list<std::shared_ptr<A>, std::allocator<std::shared_ptr<A> > >::_M_erase (this=0x23008, __position=...)
at /toolchain/arm-linux-4.8.2/arm-linux-gnueabi/include/c++/4.8.2/bits/stl_list.h:1573
#9 0x0001107c in std::list<std::shared_ptr<A>, std::allocator<std::shared_ptr<A> > >::remove (this=0x23008, __value=...)
at /toolchain/arm-linux-4.8.2/arm-linux-gnueabi/include/c++/4.8.2/bits/list.tcc:254
#10 0x00010ca0 in A::Delete (this=0x23008) at test_delete.cpp:23
#11 0x00010d64 in main () at test_delete.cpp:35
第二次:
(gdb) where
#0 A::~A (this=0x23008, __in_chrg=<optimized out>) at test_delete.cpp:28
#1 0x00011db4 in std::_Sp_counted_ptr<A*, (__gnu_cxx::_Lock_policy)2>::_M_dispose (this=0x23048)
at /toolchain/arm-linux-4.8.2/arm-linux-gnueabi/include/c++/4.8.2/bits/shared_ptr_base.h:290
#2 0x0001122c in std::_Sp_counted_base<(__gnu_cxx::_Lock_policy)2>::_M_release (this=0x23048)
at /toolchain/arm-linux-4.8.2/arm-linux-gnueabi/include/c++/4.8.2/bits/shared_ptr_base.h:144
#3 0x00010f3c in std::__shared_count<(__gnu_cxx::_Lock_policy)2>::~__shared_count (this=0xbefffcb4, __in_chrg=<optimized out>)
at /toolchain/arm-linux-4.8.2/arm-linux-gnueabi/include/c++/4.8.2/bits/shared_ptr_base.h:546
#4 0x00010e44 in std::__shared_ptr<A, (__gnu_cxx::_Lock_policy)2>::~__shared_ptr (this=0xbefffcb0, __in_chrg=<optimized out>)
at /toolchain/arm-linux-4.8.2/arm-linux-gnueabi/include/c++/4.8.2/bits/shared_ptr_base.h:781
#5 0x00010e70 in std::shared_ptr<A>::~shared_ptr (this=0xbefffcb0,
__in_chrg=<optimized out>)
at /toolchain/arm-linux-4.8.2/arm-linux-gnueabi/include/c++/4.8.2/bits/shared_ptr.h:93
#6 0x00010cac in A::Delete (this=0x23008) at test_delete.cpp:23
#7 0x00010d64 in main () at test_delete.cpp:35
相关文章推荐
- mysql数据库user表host字段的%问题
- BZOJ 1856 [Scoi2010]字符串 组合数
- 栈的基本操作
- 关于 tlb 文件
- Attempting to start Apache service...but apache doesn't work
- shared_ptr Analysis
- 2015年秋季阅读笔记计划
- win7配置自己的IIS服务器亲自做的图文很详细
- BZOJ 1854 [Scoi2010]游戏 二分图最大匹配
- [LeetCode 283] Move Zeroes
- 协同过滤的基本思想
- OpenSSL和OpenSSH
- javaWebSerivice学习篇1-WebService的基础
- 2015年秋季个人阅读计划
- **Palindrome Partitioning II
- hdoj3400Line belt【三分法】
- HDU 5451
- Binary Tree Zigzag Level Order Traversal 解答
- 雅虎-雷鳥-海猴
- ant使用手册