Утечка памяти в программе построения/удаления графа

Нужно написать программу которая строит граф по входу: А - создать граф с заданным количеством вершин n - номер новой вершины графа , (звенья выходящие из этой вершины и вес (weight) этого звена) D - delete node Нужно также вычислять разные shortest path Запустил пару тестов , все работает , но когда добавляю D , valgrind выдает ошибку Подозреваю что проблема в delete_node_cmd , хотя вижу что вершина и ее звенья стираются . Помогите найти ошибку . Спасибо ! Input : A 4 n 0 2 5 3 3 n 2 0 4 1 1 n 1 3 7 0 2 n 3 D 2

valgrind --leak-check=full --error-exitcode=1 ./graph < ../inputs/input2.txt ==1345== Using Valgrind-3.18.1 and LibVEX; rerun with -h for copyright info ==1345== Command: ./graph ==1345== insert node: 0 insert node: 2 insert node: 1 insert node: 3 Node 1: D= 3: W= 7, D= 0: W= 2, Node 3: Node 2: D= 0: W= 4, D= 1: W= 1, Node 0: D= 2: W= 5, D= 3: W= 3, delete node 2: Node 1: D= 3: W= 7, D= 0: W= 2, Node 3: Node 0: D= 3: W= 3, ==1345== ==1345== HEAP SUMMARY: ==1345== in use at exit: 48 bytes in 2 blocks ==1345== total heap usage: 12 allocs, 10 frees, 5,360 bytes allocated ==1345== ==1345== 48 (24 direct, 24 indirect) bytes in 1 blocks are definitely lost in loss record 2 of 2 ==1345== at 0x4848899: malloc (in /usr/libexec/valgrind/vgpreload_memcheck-amd64-linux.so) ==1345== by 0x109D2C: insert_node_cmd (graph.c:131) ==1345== by 0x109B19: build_graph_cmd (graph.c:46) ==1345== by 0x109270: main (main.c:12) ==1345== ==1345== LEAK SUMMARY: ==1345== definitely lost: 24 bytes in 1 blocks ==1345== indirectly lost: 24 bytes in 1 blocks ==1345== possibly lost: 0 bytes in 0 blocks ==1345== still reachable: 0 bytes in 0 blocks ==1345== suppressed: 0 bytes in 0 blocks ==1345== ==1345== For lists of detected and suppressed errors, rerun with: -s ==1345== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 0 from 0)

graph.c :

#include <stdio.h>
#include <stdlib.h>
#include "graph.h"



void deleteGraph_cmd(pnode *head){
    //head of the graph 
    pnode currentnode = *head;
    while (currentnode!=NULL){ //run above all nodes and delete its edges
       pedge currentedges = currentnode->edges;
       while (currentedges!=NULL){
            pedge temp = currentedges;
            currentedges = currentedges->next;
            free(temp);
       }
       pnode temp = currentnode;
       currentnode=currentnode->next;
       free(temp);
    }
    *head=NULL;
}



void build_graph_cmd(pnode *head){
    deleteGraph_cmd(head);
    int size = 0;
    scanf("%d", &size);  //input of number of nodes
    char c= 'C';
    scanf("%c", &c);  //insert nodes size times
    for (int i = 0; i < size; ++i)
    {
        scanf("%c", &c);
        insert_node_cmd(head);
    }
}


pnode check(pnode *head, int data){
    //printf("In the check data: %d \n",id);
    pnode temp = *head;
    while(temp!=NULL) { 
        if(temp->node_num==data){
            return temp;}
        
        temp = temp->next;   
    }
    return NULL;

}



void insert_node_cmd(pnode *head){

    int data = -1;
    scanf("%d", &data); //Build new node 
    //printf("check  startt of insert node d= %d \n",data );
printf("insert node: %d\n",data);

    pnode node_c= check(head,data);
    
    if (node_c== NULL){ //create a new node
        node_c= (pnode)malloc(sizeof(node));
        if (node_c== NULL)
        {
            return;
        }
        node_c->node_num = data;
        node_c->next = *head;
        node_c->edges = NULL;
        *head = node_c;
    }
    else{
        pedge edge_r = node_c->edges;  //free the edges
        while (edge_r != NULL){
            pedge temp = edge_r->next;
            free(edge_r);
           edge_r = temp;
        }
        node_c->edges = NULL;
    }
   // pedge first_edge=NULL;
    pedge *insert_edge = &(node_c->edges);
    //first_edge = *insert_edge;


    int dest = -1;
  
        while(scanf("%d",&dest)!= 0)
        {
        pnode new_node = check(head,dest); 
        //printf("Check in while dest %d \n",dest);
        if (new_node == NULL){
            new_node = (pnode)malloc(sizeof(node));
            if (new_node == NULL){
                return;
            }
            new_node->node_num = dest;
            new_node->edges = NULL;
            new_node->next = *head;
            *head = new_node;
        }
        int weight = -1;
        scanf("%d", &weight);

        *insert_edge = (pedge)malloc(sizeof(edge));
        if ((*insert_edge) == NULL){
            return;
        }
        (*insert_edge)->weight = weight;
        (*insert_edge)->dest = new_node;
        (*insert_edge)->next = NULL;
        insert_edge = &((*insert_edge)->next);
        
    }

}



void delete_node_cmd(pnode *head){

    int data=-1;
    scanf("%d", &data);
    printf("delete node %d:\n",data);
    pnode temp = *head;
    while (temp != NULL) {
        if (temp->edges != NULL &&temp->edges->dest->node_num == data) {  //getting to the node to delete
            pedge edge_to_remove = temp->edges;  ///save the edges of the node
            temp->edges = temp->edges->next;
            free(edge_to_remove); //delete only the edges

        }
        temp = temp->next;
    }

    //find the node and delete the node and update the next node current
    pnode temp2 = *head;
    pnode next = *head;
    next = temp2->next;
    while (next) {
        if (next->node_num == data) {
            temp2->next = next->next;
            free(next);
            next = temp2;

        }
        else {
            temp2 = next;
            next = temp2->next;
        }
    }
}


void printGraph_cmd(pnode head){  //self check dont require
    pnode n = head; 
    while (n != NULL){
        printf("Node %d: ", n->node_num); 
        pedge e = n->edges;
        while (e != NULL){
            printf("D= %d: W= %d, ", e->dest->node_num, e->weight); 
            e = e->next;
        }
        printf("\n");
        n = n->next;
    }

}



     

Ответы (0 шт):