【问题标题】:Memory leak in program with class inheritance in c++c++ 中具有类继承的程序中的内存泄漏
【发布时间】:2014-04-01 12:52:01
【问题描述】:

下面是sn-p的代码:

class Create_size_one_Nodes  : public Adjacency_list
{
public:
    Create_size_one_Nodes()
    {
        nodes_hashtable = NULL;                 
        //adds the nodes present in the graph to the hashtable
        create_nodes_hashtable();   
    }

    ~Create_size_one_Nodes()
    {
        delete(nodes_hashtable);
        nodes_hashtable = NULL;
        count_size_one_nodes_created = 0;
    }
};

class Adjacency_list
{
protected :
//create a hash table that collects the nodes already created
//the key of the hashtable is the start value of the node
//the end of the node acts as the unique object indentifier
Hashtable *nodes_hashtable;

//given the hashtable,updates the values in the hashtable
void create_nodes_hashtable();
public :

//creates the adjacency list
Adjacency_list()
{   
    nodes_hashtable = NULL;

}

//after the adjcency list has been created
//clears the contents of the adjacency list
~Adjacency_list()
{
    delete(nodes_hashtable);
    nodes_hashtable = NULL;
}
};

//given the hashtable,updates the values in the hashtable
void Adjacency_list ::  create_nodes_hashtable()
{
//remove the pointer from the previous set position
nodes_hashtable = new(std::nothrow) Hashtable(WINDOW_LENGTH *HASHTABLE_SCALING_FACTOR);

  //check :if the hashtable has been created
  if(NULL == nodes_hashtable)
  {
    string error_message = "void Adjacency_list ::  create_nodes_hashtable() , the hash table hasnot been created";
    cerr<<error_message<<endl;
  }

for(unsigned int i = 0;i< no_of_valid_nodes ;i++)
{       
    //normalising the key ,so that the key lies within the range of the hashtable
    int key = get_nodes_hashtablekey(node_start[i],node_end[i]);
    (*nodes_hashtable).add_element(key,node_end[i],i);
}

}

[更新]:

class Hashtable
{

private :

//counts the number of elements added into the hashtable
unsigned int count_elements_added;

//counts the number of elements removed from the hashtable
unsigned int count_elements_removed;

//counts the number of elements present in the hashtable
unsigned int count_elements_present;

//sets the size of the hashtable
unsigned int hashtable_size;

//the data structure (vector) that contains the objects
//the position on the hastable is defined by 2 keys
//one the position in the array of the hashtable : the start of the node is used
//the second is the first element in the pair present in the hash table //end of the node is used
std :: vector< std :: vector<std :: pair<int,int> > > hashtable;

//intialize the hashtable
void intialize_hashtable();

//checks whether the hashtable is corrupted or not
//returns true,if the hashtable is corrupted
//else returns false
bool is_corrupt();

public :

Hashtable()
{
    hashtable_size = DEFAULT_HASHTABLE_SIZE;
    hashtable.clear();
    intialize_hashtable();

    //counts the number of elements added into the hashtable
    count_elements_added = 0;

    //counts the number of elements removed from the hashtable
    count_elements_removed = 0;

    //counts the number of elements present in the hashtable
    count_elements_present = 0;
};

Hashtable(int hash_table_size)
{
    hashtable.clear();
    hashtable_size = hash_table_size;
    intialize_hashtable();

    //counts the number of elements added into the hashtable
    count_elements_added = 0;

    //counts the number of elements removed from the hashtable
    count_elements_removed = 0;

    //counts the number of elements present in the hashtable
    count_elements_present = 0;
};

//add elemnet to the hashtable
void add_element(int key,int object_identifier,int object_info);

//given the key and the object identifier
//returns the object info
int get_element(int key,int object_identifier);

//delete the element from the hashtable
void remove_element(int key,int object_identifier);

//prints the contents of the hashtable
void print();

~Hashtable()
{
    hashtable_size = 0;
    hashtable.clear();

    //counts the number of elements added into the hashtable
    count_elements_added = 0;

    //counts the number of elements removed from the hashtable
    count_elements_removed = 0;

    //counts the number of elements present in the hashtable
    count_elements_present = 0;

};
};

create_size_one_nodes 的对象是在 main 中创建的。 但是当它超出范围时,内存不会被释放。

Create_size_one_Nodes create_size_one_Nodes_object;
create_size_one_Nodes_object.create_nodes_size_one();

我无法删除 delete(nodes_hastable) 中的内存。 valgrid 指出代码中的泄漏。 vakgrind 的输出是:

==16451== 27,692 (28 direct, 27,664 indirect) bytes in 1 blocks are definitely lost in loss record 7 of 7
==16451==    at 0x402A208: operator new(unsigned int, std::nothrow_t const&) (in /usr/lib/valgrind/vgpreload_memcheck-x86-linux.so)
==16451==    by 0x80E05FF: Adjacency_list::create_nodes_hashtable() (adjacency_list.cpp:75)

请指导我如何消除此内存泄漏

【问题讨论】:

  • 显示更多代码,尤其是create_nodes_hashtable()node_hastables 的类型是什么?
  • 您的constructordestructor 都是私人的。我很惊讶你是如何从课外创建Create_size_one_Nodes 的对象。
  • @MichaelWalz 我同意。我有一种感觉,nodes_hashtable 只是一个指向数组的指针,它不会遍历和删除该数组中分配的任何内存。
  • ~Adjacency_list() 应该是虚拟的...
  • Hashtable 是什么?

标签: c++ pointers memory-management memory-leaks valgrind


【解决方案1】:

您需要提供一些额外的信息。

nodes_hashtable 是什么类型的? creat_nodes_hashtable 方法。

可能有几处错误,但没有更多信息就不可能知道:

a) nodes_hashtable 实际上是一个数组,正确的释放方式是delete[] nodes_hashtable;

b) create_nodes_hashtable 实际上正在做更多我们不知道的分配,或者正在分配东西而不是将其分配给 nodes_hashtable 或其他任何东西。

问候, 五、塞吉

【讨论】:

  • 我试过 delete[] nodes_hashtable。它仍然没有清除内存。Valgrind 仍然指示内存泄漏。
  • 其将数据添加到哈希表中,如更新代码所示。
  • 您需要提供一个自包含的测试用例,即可以编译和执行的东西,它以最少的代码展示了错误(例如,没有其他不能证明这一点的代码) .我经常发现尝试用最少的代码重现错误会导致找到它们。 OTOH 正如有人建议你应该将你的析构函数声明为虚拟的(90% 的情况下它是你想要的)。
  • 我将基类析构函数设为虚拟,它没有帮助。
  • 提供一个可编译的测试用例,如果没有关于你的对象如何交互的更多信息,就不可能知道泄漏在哪里(例如,你真的要删除你正在创建的 Adjacency_list 对象吗?)。
【解决方案2】:
  • 将基类析构函数设为 Virtual
  • 您实际上不需要在此处删除 nodes_hashtable,因为它仅在调用 create_nodes_hashtable() 函数时分配,即在 Create_size_one_Nodes 析构函数中执行它

    //after the adjcency list has been created //clears the contents of the adjacency list ~Adjacency_list() { /* delete here is not required as create_nodes_hashtable() is not called inside this class. Ideally whoever allocated memory should free.But No harm if you keep */ delete(nodes_hashtable);
    nodes_hashtable = NULL; }

【讨论】:

  • :对代码进行了相关修改。它没有帮助
  • 我刚刚检查了你的哈希表定义,它是包含向量对的向量,我不确定 hashtable.clear() 是否也会在内部向量上调用 clear()。
  • 如果可能,您可以发布程序的哪个部分报告 valgrind 中的内存泄漏
猜你喜欢
  • 2011-10-04
  • 2020-06-23
  • 2021-03-11
  • 1970-01-01
  • 1970-01-01
  • 2015-11-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多