复制构造函数导致内存泄漏

时间:2013-05-14 19:48:43

标签: c++ memory-leaks copy-constructor

我已经检查了this post,并尝试了所提到的建议,但我没有得到内存泄漏,而是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)

有什么想法吗?谢谢!

2 个答案:

答案 0 :(得分:2)

问题在于复制赋值运算符:this->Key = new Key(*(other.Key));泄漏了先前分配的Key对象。

最好的C ++ - 惯用解决方案是不使用new分配的项目。只需按值存储KeyKeys属性,问题就会消失。

答案 1 :(得分:0)

您的Key字段似乎被声明为Key *Key;。 你应该在RecordWithKey的析构函数中delete Key;