Turk Hussein
Turk Hussein

Reputation: 15

Unable to free memory in my C implementation of graph which uses strdup

Below is my data structure of graph:

typedef  struct EdgeNode{
     int adjvex;
     struct EdgeNode *nextarc;
}ENode;
typedef struct VertexNode{
     char *data; 
     char *gcc;
     int  multi_gcc;
     int  is_target;
     ENode *firstarc; 
}VNode;
typedef  struct MyGraph{
     VNode vertices[100];
}Graph;

Then I first initialize the gcc field of all the vertices to NULL:

  if (get_pos(*pGraph, target[j],i) == -1 && target[j][0] != '\n' && target[j][0] != '\0'){
    pGraph->vertices[i].data = malloc(strlen(target[j])+1);
    strcpy(pGraph->vertices[i].data, target[j]);
    pGraph->vertices[i].gcc = NULL;
    if (j == 0)
      pGraph->vertices[i].is_target = 1;
    else{
      pGraph->vertices[i].is_target = 0;
    }
    pGraph->vertices[i].firstarc = NULL;
    pGraph->vertices[i].multi_gcc = 0;
    i++;
  }

For some specific Vertices in graph, I change the gcc field to other string:

else{
      new_command = 1;
      int tmp;
      tmp = get_pos(*pGraph,my_target,i); 
      char* storage = (char *)malloc(sizeof(char)*need);
      char* real_gcc = (char *)malloc(sizeof(char)*need);
      strcpy(storage,target[0]);
      for(j = 1; j < target_id; ++j) {
        strcat(storage, " " );
        strcat(storage, target[j]);
      }
      if (new_command){
      if (pGraph -> vertices[tmp].gcc == NULL){
        pGraph->vertices[tmp].gcc = strdup(storage); //valgrind error
      }else{
        printf("the gcc is %s\n",pGraph->vertices[tmp].gcc);
        strcpy(real_gcc,pGraph->vertices[tmp].gcc);
        strcat(real_gcc,"*");
        strcat(real_gcc,storage);
        pGraph->vertices[tmp].gcc = strdup(real_gcc); //valgrind error
        if (pGraph->vertices[tmp].multi_gcc == 0){
        pGraph->vertices[tmp].multi_gcc = 1;
      }
      }
    }           



      free(storage);
      free(real_gcc);
    }

valgrind gives such messages, happens where I marked "valgrind error" on the code segment before:

==1674== 28 bytes in 1 blocks are definitely lost in loss record 1 of 2
==1674==    at 0x4C2AB80: malloc (in *)
==1674==    by 0x4EBFB49: strdup (strdup.c:42)
==1674==    by 0x402343: main (mymake.c:456)
==1674== 
==1674== 56 bytes in 1 blocks are definitely lost in loss record 2 of 2
==1674==    at 0x4C2AB80: malloc (in *)
==1674==    by 0x4EBFB49: strdup (strdup.c:42)
==1674==    by 0x402422: main (mymake.c:466)

I just don't see the reason why there's memory leak, and looks like it happens only on the last vertex. Any help?

Upvotes: 0

Views: 126

Answers (1)

dbush
dbush

Reputation: 224437

You're leaking memory because you're overwriting a pointer to malloc'ed memory:

  if (pGraph -> vertices[tmp].gcc == NULL){
    pGraph->vertices[tmp].gcc = strdup(storage);
  }else{
    // pGraph->vertices[tmp].gcc points to a valid memory location
    printf("the gcc is %s\n",pGraph->vertices[tmp].gcc);
    strcpy(real_gcc,pGraph->vertices[tmp].gcc);
    strcat(real_gcc,"*");
    strcat(real_gcc,storage);
    // ** pGraph->vertices[tmp].gcc is overwritten **
    pGraph->vertices[tmp].gcc = strdup(real_gcc);
    if (pGraph->vertices[tmp].multi_gcc == 0){
    pGraph->vertices[tmp].multi_gcc = 1;
  }

You need to free the memory before you assign a new value to this pointer.

    printf("the gcc is %s\n",pGraph->vertices[tmp].gcc);
    strcpy(real_gcc,pGraph->vertices[tmp].gcc);
    strcat(real_gcc,"*");
    strcat(real_gcc,storage);
    free(pGraph->vertices[tmp].gcc);
    pGraph->vertices[tmp].gcc = strdup(real_gcc);

Upvotes: 1

Related Questions