的向量的對象時:內存泄漏刪除保持我有以下類,<code>Tuple</code>串
class Tuple {
public:
Tuple(){
}
Tuple(vector<string> newValueList){
this->values = newValueList;
}
~Tuple() {
}
private:
vector<string> values;
}
當我調用析構函數方法我得到了內存泄漏(使用的valgrind):
Invalid read of size 8
at 0x40BE66: std::vector<std::string, std::allocator<std::string> >::~vector()
by 0x40BB2D: Tuple::~Tuple()
我不確定爲什麼會發生這種情況。我認爲這些載體做了他們自己的記憶管理。
編輯:
下面是我如何創建一個Tuple
一個例子:
Tuple* Tuple::duplicate(string value, int count, bool pull){
Tuple* returnTuple = 0;
vector<string> newValueList;
for (size_t i = 0; i < this->values.size(); i++) {
if (((int)i == count)&&!pull)
continue;
else{
newValueList.push_back(this->values[i]);
}
}
returnTuple = new Tuple(newValueList);
return returnTuple;
}
你如何創建你的'Tuple'對象? –
這個代碼本身沒有錯。 – chris
雖然你不需要明確地設置'this'的值,當你在一個成員函數裏面時^^可能只是'values = newValueList'。雖然這顯然不是問題。 –