#include <vector>
struct B
{
B() : i(10) {}
int i;
};
struct A
{
B* m_p;
A( B* p ) : m_p (p) {}
~A(){ Release(); } //问题出在这.
void Release() { delete m_p; m_p = NULL; }
};
void main()
{
std::vector<A> Ctor;
B* p = new B;
Ctor.push_back( A(p) );
B* p2 = Ctor.back().m_p;
struct B
{
B() : i(10) {}
int i;
};
struct A
{
B* m_p;
A( B* p ) : m_p (p) {}
~A(){ Release(); } //问题出在这.
void Release() { delete m_p; m_p = NULL; }
};
void main()
{
std::vector<A> Ctor;
B* p = new B;
Ctor.push_back( A(p) );
B* p2 = Ctor.back().m_p;
}
p2 成为悬空指针. 看来清楚明白统一的资源释放是必要的.
-----------------------------------------------------------
每天进步一点