When I create the following C ++ / CLI code in VS2008, warning code analysis CA1001 is displayed.
ref class A
{
public:
A() { m_hwnd = new HWND; }
~A() { this->!A(); }
protected:
!A() { delete m_hwnd; }
HWND* m_hwnd;
};
ref class B
{
public:
B() { m_a = gcnew A(); }
protected:
A^ m_a;
};
warning: CA1001: Microsoft.Design: Embed IDisposable on "B" because it creates the following IDisposable types: "A".
To fix this warning, I would have to add this code to class B:
~B() { delete m_a; }
But I do not understand why. Class A implements IDisposable through its destructor (and finalizer).
So whenever A gets the garbage collected, then a finalizer or destructor will be called, freeing up its unmanaged resources.
B 'delete' A?
GC , B "delete m_a"?
: , , A, :
ref class B
{
public:
B() { }
protected:
A m_a;
};
.
GC A ^, ?