链表中的内存泄漏

时间:2019-06-14 01:01:30

标签: memory memory-leaks linked-list malloc

我最近尝试在C中实现并发排序的链表。唯一的问题是我已经很长时间没有使用C了,尽管我的代码运行良好,但我通过valgrind发现我的代码在插入函数中存在内存泄漏这会将一个节点添加到链接列表。我的问题是我在函数的开头分配了要插入的新节点,并仔细遵循了新节点的所有用法,但是我还没有发现内存泄漏发生在哪里。找到帮助的任何帮助。我将发布插入功能。如果有人要求更多代码,我将发布更多代码。

代码:

 struct node {
    int value;
    node* next;
    pthread_mutex_t lock; // hand over hand implies a lock for every 
                          // node
 };

struct list {
   node* head;
};

  // insert function for a new value if a value already exists then do 
  nothing

  void insert_value(list* list, int value)
{
if(list == NULL) return; // if list pointer is NULL then do nothing`

node* new_node = malloc(sizeof(node)); // create new node                                  

if(new_node == NULL) return; // check if allocation fails

new_node->value = value;

pthread_mutex_init(&(new_node->lock),NULL); // initialize mutex lock  
                                            // for the new node

pthread_mutex_lock(&(new_node->lock)); // lock the new node

if(list->head == NULL) // new node is the first element in the list
{
     new_node->next = NULL;
     list->head = new_node;
     pthread_mutex_unlock(&(new_node->lock));
     return;
}

pthread_mutex_lock(&(list->head->lock)); // lock the head of the list

node* temp;

if(list->head->value == new_node->value) // do nothing if the head of 
                                         // list is equal to new node

{
   pthread_mutex_unlock(&(list->head->lock));
   pthread_mutex_unlock(&(new_node->lock));
   pthread_mutex_destroy(&(new_node->lock));
   free(new_node);
   return;
}

else if(list->head->value > new_node->value) // new node comes before 
                                             // the list head
{
   new_node->next = list->head;
   temp = list->head;
   list->head = new_node;
   pthread_mutex_unlock(&(list->head->lock));
   pthread_mutex_unlock(&(temp->lock));
   return;
}

else
{
    // Locate the node before the point of insertion //

    node* dummy;
    node* current = list->head;
    temp = current->next; 

    if(temp == NULL) // new node comes after
                     // the list head
    {
        new_node->next = current->next;
        current->next = new_node;
        pthread_mutex_unlock(&(new_node->lock));
        pthread_mutex_unlock(&(current->lock));
        return;
    }

   pthread_mutex_lock(&(temp->lock)); // lock the successor of the head

   // perform hand over hand traversal of the list
   // and check if temp reaches the end of the list (NULL)

    while (temp->value < new_node->value)
    {
      pthread_mutex_unlock(&(current->lock));
      current = temp;
      temp = temp->next;
      if(temp == NULL) break;
      pthread_mutex_lock(&(temp->lock));
    }

    if(temp == NULL) // new node will be the last element in this case 
    {
        current->next = new_node;
        new_node->next = NULL;
        pthread_mutex_unlock(&(current->lock));
        pthread_mutex_unlock(&(new_node->lock));
        return;
    }

    else if(temp->value == new_node->value) //new node already exists 
    {
       pthread_mutex_unlock(&(temp->lock));
       pthread_mutex_unlock(&(current->lock));
       pthread_mutex_unlock(&(new_node->lock));
       pthread_mutex_destroy(&(new_node->lock));
       free(new_node);
       return;
    }

    else // new node should be inserted inside the list
    {
       dummy = temp;
       new_node->next = current->next;
       current->next = new_node;
       pthread_mutex_unlock(&(dummy->lock));
       pthread_mutex_unlock(&(current->lock));
       pthread_mutex_unlock(&(new_node->lock));
       return;
    }
}
 } `

0 个答案:

没有答案