在以下代码中发生内存泄漏,我对此表示怀疑。在 test() 乐趣中:
#include <string>
#include <iostream>
#include <boost/shared_ptr.hpp>
#include <boost/weak_ptr.hpp>
class parent;
class children;
typedef boost::shared_ptr<parent> parent_ptr;
typedef boost::shared_ptr<children> children_ptr;
class parent
{
public:
~parent() { std::cout <<"destroying parent\n"; }
public:
children_ptr children;
};
class children
{
public:
~children() { std::cout <<"destroying children\n"; }
public:
parent_ptr parent;
};
void test()
{
parent_ptr father(new parent());
children_ptr son(new children);
father->children = son;// parent_ptr_count=1, children_ptr_count=2
son->parent = father;// parent_ptr_count=2, children_ptr_count=2
//1,2,3 See the note below
}
void main()
{
std::cout<<"begin test...\n";
test();
std::cout<<"end test.\n";
}
// childern_ptr 从堆栈中弹出,我认为 childern_ptr_count-- 和 parent_ptr_count--
// parent_ptr 从堆栈中弹出,我认为 childern_ptr_count-- 和 parent_ptr_count--
// 但实际上它并没有这样做,为什么?
我希望有人可以帮助我,非常感谢。