【发布时间】:2019-12-18 21:24:20
【问题描述】:
我有这个简单的代码
#include<memory>
class SLLNode{
public:
SLLNode(const int& d){
data_ = d;
}
std::shared_ptr<SLLNode> next_;
int data_;
};
int main(){
std::shared_ptr<SLLNode> head(new SLLNode(1));
head->next_.reset(new SLLNode(2));
*head = *(head->next_);
return 0;
}
Valgrind 抱怨我的读取无效。
==17312== Invalid read of size 4
==17312== at 0x108EF3: SLLNode::operator=(SLLNode const&) (test_shared_ptr.cpp:3)
==17312== by 0x108DA3: main (test_shared_ptr.cpp:16)
==17312== Address 0x5b7dd50 is 16 bytes inside a block of size 24 free'd
==17312== at 0x4C3123B: operator delete(void*) (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==17312== by 0x109511: std::_Sp_counted_ptr<SLLNode*, (__gnu_cxx::_Lock_policy)2>::_M_dispose() (shared_ptr_base.h:376)
==17312== by 0x1090DF: std::_Sp_counted_base<(__gnu_cxx::_Lock_policy)2>::_M_release() (shared_ptr_base.h:154)
==17312== by 0x109061: std::__shared_count<(__gnu_cxx::_Lock_policy)2>::operator=(std::__shared_count<(__gnu_cxx::_Lock_policy)2> const&) (shared_ptr_base.h:703)
==17312== by 0x108E9A: std::__shared_ptr<SLLNode, (__gnu_cxx::_Lock_policy)2>::operator=(std::__shared_ptr<SLLNode, (__gnu_cxx::_Lock_policy)2> const&) (shared_ptr_base.h:1034)
==17312== by 0x108EC4: std::shared_ptr<SLLNode>::operator=(std::shared_ptr<SLLNode> const&) (shared_ptr.h:93)
==17312== by 0x108EEE: SLLNode::operator=(SLLNode const&) (test_shared_ptr.cpp:3)
==17312== by 0x108DA3: main (test_shared_ptr.cpp:16)
==17312== Block was alloc'd at
==17312== at 0x4C3017F: operator new(unsigned long) (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==17312== by 0x108D5C: main (test_shared_ptr.cpp:15)
我使用的命令是:
valgrind --tool=memcheck ./test_shared_ptr
=operator 似乎仅在我使用嵌套 shared_ptr 时才会导致此问题。 我期待来自 ptr head 的数据被释放并替换为来自 ptr head.next_ 的数据,但似乎发生了其他事情,因为我有这个无效的读取。 我想替换 head 指向的数据(而不是 head 本身),因为这个 head ptr 可能是 head 的另一个 shared_ptr 的副本。
【问题讨论】:
标签: c++ c++11 valgrind shared-ptr