【问题标题】:Copy constructor causes memory leak复制构造函数导致内存泄漏
【发布时间】:2013-05-15 04:36:30
【问题描述】:

我检查了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)

有什么想法吗?谢谢!

【问题讨论】:

  • 我没有看到析构函数、赋值运算符、(或移动版本)或 RAII。
  • Key::Keys[] 的值是否在某处被删除?
  • Key = new Key(*(other.Key)); 在构造函数中加入 Key = NULL; == 可能取消引用 nullptr。
  • 对于我看到的每个new,我应该在某处看到delete
  • Key::Keys 是如何声明的?

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


【解决方案1】:

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

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

【讨论】:

    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 2021-06-17
      • 1970-01-01
      • 2015-06-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-01-12
      • 2021-07-07
      相关资源
      最近更新 更多