我有以下应用程序结构:
/// CLASS VIEW3D.h
class View3D;
typedef boost::shared_ptr<View3D> ViewSP;
class View3D
{
public:
View3D(void);
};
/// CLASS SCREENQUAD.h
class ScreenQuad
{
public:
ScreenQuad(ViewSP view);
~ScreenQuad(void);
protected:
ViewSP _viewSP;
};
/// CLASS VIEW3D.cpp
View3D::Init(ViewSP view)
{
_screenQuadSP=new ScreenQuad(view);
}
/// 类 SCREENQUAD.cpp
ScreenQuad::ScreenQuad(ViewSP view):
_viewSP(view)
{
assert(_viewSP);
}
现在,我将类的引用以共享指针的形式传递给A
类B
,并将其保存在全局变量A_SP
中。当应用程序关闭时,我得到了这个:
HEAP: Free Heap block 2837920 modified at 2837b5c after it was freed
调试执行后我发现类A
析构函数被调用后,当类的析构函数B
被执行时,它会再次被调用。所以我的猜测是boost试图释放封装在指针地址处的内存_A_ref
。只是要注意:销毁的顺序是A
先类,然后是B
类。
我该如何解决?不应该shared_ptr
保持引用计数并且不触发已经释放的对象的析构函数吗?