Why won't the GC automatically distribute members of my class?

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 ^, ?

+3
1

. . . http://msdn.microsoft.com/en-us/library/ms177197.aspx

ref class B
{
public:
    B()   {}
    ~B()  {}
protected:
    A    m_a;
};

- . .

Edit:

.net , # , Dispose , , ++ .

++/cli . , , dispose, ++/cli .

- , , #, .

A ^, , .

, , IDispose. .

GC . . , , .

, .

+2

Source: https://habr.com/ru/post/1714969/


All Articles