C Memory Leak Problem -


your program did not free of memory allocated ==17711== memcheck, memory error detector ==17711== copyright (c) 2002-2009, , gnu gpl'd, julian seward et al. ==17711== using valgrind-3.5.0 , libvex; rerun -h copyright info ==17711== command: ./counter ==17711== ==17711== conditional jump or move depends on uninitialised value(s) ==17711==    @ 0x400b98: main (counter.c:105) ==17711== ==17711== conditional jump or move depends on uninitialised value(s) ==17711==    @ 0x400bdb: main (counter.c:107) ==17711== ==17711== conditional jump or move depends on uninitialised value(s) ==17711==    @ 0x400c1b: main (counter.c:109) ==17711== error ==17711== ==17711== heap summary: ==17711==     in use @ exit: 64 bytes in 4 blocks ==17711==   total heap usage: 4 allocs, 0 frees, 64 bytes allocated ==17711== ==17711== leak summary: ==17711==    lost: 0 bytes in 0 blocks ==17711==    indirectly lost: 0 bytes in 0 blocks ==17711==      possibly lost: 0 bytes in 0 blocks ==17711==    still reachable: 64 bytes in 4 blocks ==17711==         suppressed: 0 bytes in 0 blocks ==17711== rerun --leak-check=full see details of leaked memory ==17711== ==17711== counts of detected , suppressed errors, rerun with: -v ==17711== use --track-origins=yes see uninitialised values come ==17711== error summary: 3 errors 3 contexts (suppressed: 6 6) make: *** [test_counter] error 1 

i have memory leak in program... pretty sure free dynamic memory.. don't understand leak =\

#include "counter.h"  /* ============================================================================  * file-global variables  * ========================================================================== */ static int ncounters = 0; static struct counter *counters = null;  static int nthreads = 0; static int *ninstructions = null; static struct instruction **instructions = null;   /* ============================================================================  * operations  * ========================================================================== */ static void decrement(long long *n) {         (*n)--; }  static void increment(long long *n) {     (*n)++; }  static void mult2(long long *n) {     (*n)=(*n)*2; }   /* ============================================================================  * helper functions  * ========================================================================== */  pthread_mutex_t lock1;  /* ============================================================================  * thread function  * ========================================================================== */ static void * worker_thread(void *arg) {          pthread_mutex_lock( &lock1 );          long long n = (long long)arg;         for(int i=0; < ninstructions[n] ;i++){           for(int j=0; j < instructions[n][i].repetitions ;j++){             (*instructions[n][i].work_fn)(&((instructions[n][i].counter)->counter));           }         }         pthread_mutex_unlock( &lock1 );     return null; }   /* ============================================================================  * main function  * ========================================================================== */ int main(void) {          scanf(" %d", &ncounters); //ask number of counters          int i;          if((counters = malloc(sizeof(struct counter)*ncounters))){           for( i=0; < ncounters ;i++){             counters[i].counter = 0;           }         }          scanf(" %d", &nthreads); //ask number of threads          if((ninstructions = (int*) malloc(nthreads*sizeof(int)))){           for( i=0; < nthreads ;i++){             ninstructions[i] = 0;           }         }          instructions = malloc(nthreads*sizeof(struct instruction *));          for(i=0; < nthreads ;i++){            scanf(" %d", &ninstructions[i]);  //ask number of instructions within threads[i]            instructions[i] = malloc(ninstructions[i]*sizeof(struct instruction));           for(int j=0;j < ninstructions[i] ;j++){             int srepetition;             char sfunction;             int scounter;              scanf(" %d %c %d", &scounter, &sfunction, &srepetition);              instructions[i][j].repetitions = srepetition;              instructions[i][j].counter = (counters+scounter);              if(sfunction == 'i'){               instructions[i][j].work_fn = increment;             }else if(sfunction == 'd'){               instructions[i][j].work_fn = decrement;             }else if(sfunction == '2'){               instructions[i][j].work_fn = mult2;             }else{               printf("error\n");             }            }          }          pthread_t thread_id[nthreads];          for(long long i=0; < nthreads ;i++){           pthread_create( &thread_id[i], null, worker_thread, (void *)i);         }          for(int i=0; < nthreads ;i++){           pthread_join( thread_id[i], null);         }          for(i=0; < ncounters ;i++){           printf("%lld\n", counters[i].counter);         }          for(int i=0; i< nthreads;i++){           free(instructions[i]);         }         free(instructions);         free(ninstructions);         free(counters);     return 0; } 

you free memory instructions occupies:

free(instructions);  

then access memory:

for(int i=0 ; < nthreads ; i++){     free(instructions[i]);  }  

you cannot access instructions after free(instructions).

you need free pointers in instructions first, free instructions array.


Comments