我检查了这篇文章,并尝试了提到的建议,但我得到的不是内存泄漏segmentation faults
。
这是导致泄漏的代码:
class RecordWithKey : public VarLengthRecord {
protected:
Key* Key;
};
class Key {
protected:
KeyType keyType;
Key* Keys[2];
};
RecordWithKey& RecordWithKey::operator=(const RecordWithKey& other)
{
if (this != &other)
{
this->Key = new Key(*(other.Key));
/* ... other code ... */
}
return *this;
}
RecordWithKey::~RecordWithKey()
{
delete(Key);
Key = NULL;
}
Key::~Key()
{
for (unsigned int i = 0; i < KEY_TYPES; i++)
{
delete(Keys[i]);
Keys[i] = NULL;
}
}
Key& Key::operator =(const Key& other)
{
if (this != &other)
{
this->keyType= other.keyType;
for(unsigned int i=0;i<KEY_TYPES;i++)
(*Keys[i]) = (*other.Keys[i]);
}
return (*this);
}
RecordWithKey::RecordWithKey()
:VarLengthRecord()
{
Key = NULL;
}
RecordWithKey::RecordWithKey(const RecordWithKey& other)
: VarLengthRecord(other)
{
Key = new Key(*(other.Key));
}
Key::Key()
{
this->keyType = TYPE_STRING; //default type
this->Keys[TYPE_NUMERIC]= new NumericKey;
this->Keys[TYPE_STRING]= new StringKey;
}
Key::Key(const Key& other)
{
this->keyType = other.keyType;
this->Keys[TYPE_NUMERIC]= new NumericKey;
this->Keys[TYPE_STRING]= new StringKey;
if (this->keyType == TYPE_NUMERIC)
{
int Key;
other.get_Key(Key);
set_Key(Key); //there is no "new" inside of this
}
else if (this->keyType == TYPE_STRING)
{
std::string Key;
other.get_Key(Key);
set_Key(Key); //there is no "new" inside of this
}
}
这是来自的报告valgrind
:
==15019== 8 bytes in 1 blocks are indirectly lost in loss record 1 of 41
==15019== at 0x402B9B4: operator new(unsigned int) (in /usr/lib/valgrind/vgpreload_memcheck-x86-linux.so)
==15019== by 0x80673E6: Key::Key(Key const&) (Key.cpp:21)
==15019== by 0x806B414: RecordWithKey::RecordWithKey(RecordWithKey const&) (RecordWithKey.cpp:20)
==15019== by 0x805B8F6: __gnu_cxx::new_allocator<RecordWithKey>::construct(RecordWithKey*, RecordWithKey const&) (in /project/tests/tests)
==15019== by 0x805BA55: std::vector<RecordWithKey, std::allocator<RecordWithKey> >::_M_insert_aux(__gnu_cxx::__normal_iterator<RecordWithKey*, std::vector<RecordWithKey, std::allocator<RecordWithKey> > >, RecordWithKey const&) (vector.tcc:335)
==15019== by 0x805B370: std::vector<RecordWithKey, std::allocator<RecordWithKey> >::push_back(RecordWithKey const&) (stl_vector.h:834)
==15019== by 0x806FF73: NodoSecuencial::insertar(RecordWithKey const&, std::vector<RecordWithKey, std::allocator<RecordWithKey> >&) (NodoSecuencial.cpp:102)
==15019== by 0x8059A8E: TestNodoSecuencial::test_nodo_sec_insertar_eliminar() (TestNodoSecuencial.cpp:200)
==15019== by 0x8058400: TestNodoSecuencial::ejecutar() (TestNodoSecuencial.cpp:22)
==15019== by 0x8076D91: main (tests.cpp:261)
有任何想法吗?谢谢!