c++ - Copy constructor causes memory leak -
i've checked this post, , tried suggestions mentioned, instead of getting memory leaks segmentation faults
.
this code causes leak:
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() { (unsigned int = 0; < 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 no "new" inside of } else if (this->keytype == type_string) { std::string key; other.get_key(key); set_key(key); //there no "new" inside of } }
this report valgrind
:
==15019== 8 bytes in 1 blocks indirectly lost in loss record 1 of 41 ==15019== @ 0x402b9b4: operator new(unsigned int) (in /usr/lib/valgrind/vgpreload_memcheck-x86-linux.so) ==15019== 0x80673e6: key::key(key const&) (key.cpp:21) ==15019== 0x806b414: recordwithkey::recordwithkey(recordwithkey const&) (recordwithkey.cpp:20) ==15019== 0x805b8f6: __gnu_cxx::new_allocator<recordwithkey>::construct(recordwithkey*, recordwithkey const&) (in /project/tests/tests) ==15019== 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== 0x805b370: std::vector<recordwithkey, std::allocator<recordwithkey> >::push_back(recordwithkey const&) (stl_vector.h:834) ==15019== 0x806ff73: nodosecuencial::insertar(recordwithkey const&, std::vector<recordwithkey, std::allocator<recordwithkey> >&) (nodosecuencial.cpp:102) ==15019== 0x8059a8e: testnodosecuencial::test_nodo_sec_insertar_eliminar() (testnodosecuencial.cpp:200) ==15019== 0x8058400: testnodosecuencial::ejecutar() (testnodosecuencial.cpp:22) ==15019== 0x8076d91: main (tests.cpp:261)
any ideas? thanks!
your problem in copy assignment operator: this->key = new key(*(other.key));
leaks allocated key
object.
the best c++-idiomatic solution not using new
ly allocated items. store key
, keys
attributes value , problem goes away.
Comments
Post a Comment