考慮下面的例子:的std ::矢量銷燬和意外的內存泄漏
#include <vector>
class Foo {
std::vector<int*> v;
public:
Foo() {
this->v.push_back(new int(23));
this->v.push_back(new int(24));
this->v.push_back(new int(25));
}
~Foo() {
}
};
int main() {
Foo f;
return 0;
}
當f超出在main()範圍中,f的析構函數被調用,它應該間接自由f.v.根據this,向量v的每個元素的析構函數現在應該被調用。
但是,當我在valgrind中運行這個程序時,我發現int *沒有被釋放。
$ valgrind --leak-check=full ./a.out
我在這裏錯過了什麼?
可能重複[是否標準::目錄::刪除每個刪除元素的方法調用析構函數?] (http://stackoverflow.com/questions/4260464/does-stdlistremove-method-call-destructor-of-each-removed-element) – fredoverflow