0

I am having this error while running my program glibc detected * ./out: double free or corruption (!prev): 0x082a7dd0 the problem occurs when tryng to free a pointer, here are some parts of code:

//Structure to define an existing lightpath in the network
struct existing_lightpath{
    int* route;
    int wavelength;
    int* edges;
    int num;
    double LAR;
    double IAR;
    int LAR_group[100];
    int IAR_group[100];
    struct existing_lightpath* next_lightpath;
};

and this code for deleting the node from linked list

if(event_type_cur == 0 && existing_lightpath_list != NULL){
temp = existing_lightpath_list;
    previous = NULL;
    while(temp->num != lightpath_num_cur && temp->next_lightpath != NULL){
        previous = temp;
        temp = temp->next_lightpath;
}

next = temp->next_lightpath;
//delete the current node from the linked list
if(previous == NULL){
    if(next == NULL){
        free(temp->route);
        free(temp->edges);
        free(temp);
        temp = NULL;
        existing_lightpath_list = NULL;    
    }
    else {
        existing_lightpath_list = next;
        temp->next_lightpath = NULL;
        free(temp->route);
        free(temp->edges);
        free(temp);
    }
}
else{
    if(next == NULL){
        previous->next_lightpath = NULL;
        for(i=0; i<nodes && temp->route[i] != -1; i++)
            printf("%d ", temp->route[i]);
            free(temp->route);
            free(temp->edges);
            free(temp);
    }
    else{
        previous->next_lightpath = next;
        temp->next_lightpath = NULL;
        free(temp->route);
        free(temp->edges);
        free(temp);
    }
}

the error detected with the 3rd case: free(temp->route) when previous is not NULL but next is NULL after the printing. I'm allocating mem for linked list nodes elsewhere in the program, nothing wrong with it Thanks in advance for any help

Sakthi Kumar
  • 3,047
  • 15
  • 28
user2753651
  • 1
  • 1
  • 1
  • Always set `temp` to null after free'ing - like you do in the first case, that should make it easier to find the problem. Being able to print doesn't mean it's not freed. – auselen Sep 06 '13 at 09:33
  • Sometimes, "nothing wrong with it" isn't true. – unwind Sep 06 '13 at 09:33
  • What is `existing_lightpath_list` ? a "global" pointer to the start of the LinkedList ? BTW: there is a slight possibility that the code is actually correct, but that you should not call it twice ... – joop Sep 06 '13 at 09:36
  • See the reasons of memory corruption [here](http://en.wikipedia.org/wiki/Memory_corruption). Check if you are doing any of this in your code. Basically you must not `free` any memory which you have not allocated with `*alloc` family of functions. – 0xF1 Sep 06 '13 at 09:39
  • 3
    `valgrind` is the answer. – Dariusz Sep 06 '13 at 09:44
  • 1
    Please factor your code. All your branches do the same `free` operation. – Jens Gustedt Sep 06 '13 at 10:18
  • Is the actual array that `temp->route` points to used in another node? If you have something like `int* x = (int*)malloc(2 * sizeof(int)); node1->route = x; node2->route= x;` that would cause a double free in the code you've posted. –  Sep 06 '13 at 12:48
  • Possible duplicate of [How to track down a "double free or corruption" error](https://stackoverflow.com/questions/2902064/how-to-track-down-a-double-free-or-corruption-error) – Raedwald Dec 06 '18 at 13:57

0 Answers0