2014-02-28 61 views
0

以下类继承是代码片段:内存泄漏在C++

​​

}

[更新]:

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的目的在主创建。 但是当它超出范围时,内存不会被释放。

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) 

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

+2

显示更多代码,特别是'create_nodes_hashtable()'。什么是'node_hastables'类型? –

+0

你的构造函数和析构函数都是私有的。我很惊讶你是如何从课堂外创建'Create_size_one_Nodes'的对象的。 – rajenpandit

+0

@MichaelWalz我同意。我有一种感觉,node_hashtable只是一个指向数组的指针,它不会迭代并删除该数组中的所有已分配的内存。 – CoryKramer

回答

0

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

什么类型是nodes_hashtable? creat_nodes_hashtable方法。

可能有几件事情错的,但没有更多的信息,这是不可能知道:

一)nodes_hashtable确实是一个数组,并dealocate将delete[] nodes_hashtable;

B中的正确方法)create_nodes_hashtable实际上是做多我们不知道的分配,还是分配东西,而不是分配给nodes_hashtable或其他。

问候, 五SEGUI

+0

我试过删除[] nodes_hashtable。它仍然没有清除内存.Valgrind仍然表示内存泄漏。 – priyanka

+0

将其数据添加到散列表中,如更新代码中所示。 – priyanka

+0

您需要提供一个自包含的测试用例,也就是说,可以编译和执行的代码用最少的代码展示错误(例如,没有其他代码不能用来证明这一点)。我经常发现,试图用最少的代码重现错误会导致找到它们。 OTOH就像有人建议你应该声明你的析构函数是虚拟的(90%的情况是你想要的)。 – vseguip

0
  • 让基类的析构函数虚拟
  • 你实际上并不需要删除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; }

+0

:对代码进行了相关更改。它没有帮助 – priyanka

+0

我刚刚检查了你的散列表定义,它是向量conaining对的向量,我不确定hashtable.clear()是否也会在内部向量上调用clear()。 – Singh

+0

如果可能的话,你可以在valgrind中发布哪部分程序报告内存泄漏 – Singh