简体   繁体   English

stl 矢量 memory 管理

[英]stl vector memory management

I am using borland 2006 c++, and have following code.我正在使用 borland 2006 c++,并有以下代码。 I am using vectors, and have trouble understanding why the destructor is not being called.我正在使用向量,并且无法理解为什么不调用析构函数。

basically i have a class A基本上我有一个 class A

class A
{
private:
    TObjectList* list;
    int myid;
public:
 __fastcall A(int);
 __fastcall ~A();
};

__fastcall A::A(int num)
{
    myid = num;
    list = new TObjectList();

}

__fastcall A::~A()
{
    delete list;
}

int main(int argc, char* argv[])
{
    myfunc();
    return 0;
}

void myfunc()
{
    vector<A*> vec;
    vec.push_back(new A(1));
    vec.push_back(new A(2));
}

according to what i read, when variable vec goes out of scope in myfunc(), it should destruct its contained elements, so the destructor for A should be called.根据我的阅读,当变量 vec 在 myfunc() 中超出 scope 时,它应该破坏它包含的元素,所以应该调用 A 的析构函数。 I have a breakpoint at ~A(), but never gets called, i have tried resize(), erase methods also我在 ~A() 处有一个断点,但从未被调用,我也尝试过 resize() 和擦除方法

TIA TIA

vec does destruct its elements when it goes out of scope. The problem here is that vec's elements are the pointers to A objects, not A objects themselves. vec 在超出 scope 时确实会破坏它的元素。这里的问题是 vec 的元素是指向A 对象的指针,而不是 A 对象本身。 If you instead did如果你反而做了

vector<A> vec;
vec.push_back(A(1));
vec.push_back(A(2));

...then things would work as you expect. ...那么事情就会如你所愿。

ETA: note, though, that if you do this you have to define a copy constructor for A. That should involve doing a deep copy of the TObjectList member. ETA:不过请注意,如果你这样做,你必须为 A 定义一个复制构造函数。这应该涉及对 TObjectList 成员进行深层复制。 Otherwise, when you copy an A object you'll wind up with two objects both pointing to the same TObjectList, and your program will crash when the second object is destroyed.否则,当您复制 A object 时,您将得到两个指向同一个 TObjectList 的对象,并且当第二个 object 被销毁时,您的程序将崩溃。

The destructor for A isn't called because you don't have a vector of A .未调用A的析构函数,因为您没有A的向量。 You have a vector of pointers to A , and the destructors for the pointers are called.您有一个指向A的指针向量,并且调用了指针的析构函数。 Pointers don't have a destructor, so nothing happens.指针没有析构函数,所以什么也不会发生。

One way to delete everything would be to manually do something like删除所有内容的一种方法是手动执行类似

while (!vec.empty())
{
    delete vec.back();
    vec.pop_back();
}

Lots of good answers already, but I'll add one more:已经有很多好的答案,但我会再添加一个:

Use boost::ptr_vector from the Boost Pointer Container Library , instead of std::vector.使用来自Boost Pointer Container Libraryboost::ptr_vector ,而不是 std::vector。 It will delete the objects when the vector goes out of scope, thus calling the destructors.当 vector 超出 scope 时,它将删除对象,从而调用析构函数。

Grab the Boost libraries, and wherever you have a raw pointer in the above you use boost::shared_ptr<> instead.获取 Boost 库,在上面任何有原始指针的地方,都可以使用 boost::shared_ptr<> 代替。 (Well, not in the signature of main().) (好吧,不在 main() 的签名中。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM