vector <T *> destructor

Posted by Daniel.Z on Stack Overflow See other posts from Stack Overflow or by Daniel.Z
Published on 2012-06-01T15:23:23Z Indexed on 2012/06/01 16:40 UTC
Read the original article Hit count: 249

Filed under:
|
|
|

I have a class defined like:

Class A
{
public:
    int num;
    A *parent;
    vector<A *> children;
    ...

    // constructor without parameters
    A(void)
    {
        this->num = 3;
        this->parent = 0;
        for (int i=0;i<num;++i)
            children.push_back(new A(this,num-1));
    }

    // constructor with parameters
    A(A *a,int n)
    {
        this->num = n;
        this->children->parent = a;
        for (int i=0;i<num;++i)
            this->children.push_back(new A(this,this->num-1));
    }
};

now, the constructor works fine. there are some problem with destructor. currently, the destructor is defined as:

A::~A(void)
{
    if (this->parent!=0) this->parent = 0;
    for (int i=0;i<(int)children.size();++i)
        this->children[i]->~A();
    vector <A *> ().swap(this->children);
}

but every time when I debug it, it will break at:

void deallocate(pointer _Ptr, size_type)
    {    // deallocate object at _Ptr, ignore size
    ::operator delete(_Ptr);
    }

it looks like I cannot delete the pointer in the vector of this->children, is there any way that I can de-construct the class successfully?

© Stack Overflow or respective owner

Related posts about c++

Related posts about memory