2014-02-28 60 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