繁体   English   中英

C ++中具有类继承的程序中的内存泄漏

[英]Memory leak in program with class inheritance in c++

以下是代码段:

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)

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

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

node_hashtable是什么类型? creat_nodes_hashtable方法。

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

a)nodes_hashtable实际上是一个数组,取消分配的正确方法是delete[] nodes_hashtable;

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

此致V.Seguí

  • 将基类的析构函数设为虚拟
  • 您实际上不需要删除这里的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; }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM