title: double free or corruption (out) fclose
body: <pre><code>*** glibc detected *** /home/ghoshs/workspace/Simulator/Debug/Simulator: double free or corruption (out): 0x00000000017e6030 ***
  ======= Backtrace: =========
  /lib/x86_64-linux-gnu/libc.so.6(+0x7e626)[0x7f34b8553626]
  /lib/x86_64-linux-gnu/libc.so.6(fclose+0x155)[0x7f34b85432a5]
  /home/ghoshs/workspace/Simulator/Debug/Simulator[0x40e7bf]
  /home/ghoshs/workspace/Simulator/Debug/Simulator[0x40e94a]
  /home/ghoshs/workspace/Simulator/Debug/Simulator[0x40e357]
  /lib/x86_64-linux-gnu/libc.so.6(__libc_start_main+0xed)[0x7f34b84f676d]
  /home/ghoshs/workspace/Simulator/Debug/Simulator[0x405e19]
  ======= Memory map: ========
  00400000-0128c000 r-xp 00000000 08:06 1576650                            /home/ghoshs/workspace/Simulator/Debug/Simulator
  0148b000-0148c000 r-xp 00e8b000 08:06 1576650                            /home/ghoshs/workspace/Simulator/Debug/Simulator
  0148c000-014bd000 rwxp 00e8c000 08:06 1576650                            /home/ghoshs/workspace/Simulator/Debug/Simulator
  014bd000-014c7000 rwxp 00000000 00:00 0 
  017e6000-01807000 rwxp 00000000 00:00 0                                  [heap]
  7f34b84d5000-7f34b8688000 r-xp 00000000 08:06 4849816                    /lib/x86_64-linux-gnu/libc-2.15.so
  7f34b8688000-7f34b8887000 ---p 001b3000 08:06 4849816                    /lib/x86_64-linux-gnu/libc-2.15.so
  7f34b8887000-7f34b888b000 r-xp 001b2000 08:06 4849816                    /lib/x86_64-linux-gnu/libc-2.15.so
  7f34b888b000-7f34b888d000 rwxp 001b6000 08:06 4849816                    /lib/x86_64-linux-gnu/libc-2.15.so
  7f34b888d000-7f34b8892000 rwxp 00000000 00:00 0 
  7f34b8892000-7f34b88a7000 r-xp 00000000 08:06 4853364                    /lib/x86_64-linux-gnu/libgcc_s.so.1
  7f34b88a7000-7f34b8aa6000 ---p 00015000 08:06 4853364                    /lib/x86_64-linux-gnu/libgcc_s.so.1
  7f34b8aa6000-7f34b8aa7000 r-xp 00014000 08:06 4853364                    /lib/x86_64-linux-gnu/libgcc_s.so.1
  7f34b8aa7000-7f34b8aa8000 rwxp 00015000 08:06 4853364                    /lib/x86_64-linux-gnu/libgcc_s.so.1
  7f34b8aa8000-7f34b8ac0000 r-xp 00000000 08:06 4855760                    /lib/x86_64-linux-gnu/libpthread-2.15.so
  7f34b8ac0000-7f34b8cbf000 ---p 00018000 08:06 4855760                    /lib/x86_64-linux-gnu/libpthread-2.15.so
  7f34b8cbf000-7f34b8cc0000 r-xp 00017000 08:06 4855760                    /lib/x86_64-linux-gnu/libpthread-2.15.so
  7f34b8cc0000-7f34b8cc1000 rwxp 00018000 08:06 4855760                    /lib/x86_64-linux-gnu/libpthread-2.15.so
  7f34b8cc1000-7f34b8cc5000 rwxp 00000000 00:00 0 
  7f34b8cc5000-7f34b8dbe000 r-xp 00000000 08:06 4849815                    /lib/x86_64-linux-gnu/libm-2.15.so
  7f34b8dbe000-7f34b8fbd000 ---p 000f9000 08:06 4849815                    /lib/x86_64-linux-gnu/libm-2.15.so
  7f34b8fbd000-7f34b8fbe000 r-xp 000f8000 08:06 4849815                    /lib/x86_64-linux-gnu/libm-2.15.so
  7f34b8fbe000-7f34b8fbf000 rwxp 000f9000 08:06 4849815                    /lib/x86_64-linux-gnu/libm-2.15.so
  7f34b8fbf000-7f34b8fe1000 r-xp 00000000 08:06 4849818                    /lib/x86_64-linux-gnu/ld-2.15.so
  7f34b91bd000-7f34b91c1000 rwxp 00000000 00:00 0 
  7f34b91dd000-7f34b91e1000 rwxp 00000000 00:00 0 
  7f34b91e1000-7f34b91e2000 r-xp 00022000 08:06 4849818                    /lib/x86_64-linux-gnu/ld-2.15.so
  7f34b91e2000-7f34b91e4000 rwxp 00023000 08:06 4849818                    /lib/x86_64-linux-gnu/ld-2.15.so
  7fffc3f01000-7fffc3fab000 rwxp 00000000 00:00 0                          [stack]
  7fffc3fff000-7fffc4000000 r-xp 00000000 00:00 0                          [vdso]
  ffffffffff600000-ffffffffff601000 r-xp 00000000 00:00 0                  [vsyscall]
  </code></pre>
  
  <p>I am doing a simple 
  <code>FILE* temp_dataset = fopen(dataset_filnames,"w");</code>
  and after few write operations i am closing it using a simple
  <code>fclose(temp_dataset);</code>
  At this point I am getting the double free corruption error. In the function I am just generating two random numbers that will act as a source and a destination, which will be further used by algorithm to find routes on a graph. But that part is totally independent of this part of the code. (Sorry for the irrelevant info stack overflow would not let me post the question.) </p>
  
  <pre><code>void generate_dataset(int number_of_src_dest,
          char dataset_filnames[],NI* network_info, int k, int network_set,int dataset_num)
  {
      int i,j;
  
      printf("Generating Dataset for the Network Set...\n");
      sprintf(dataset_filnames,"src_dest_pair_#nodes_%d_#SDP_%d_NS_%d_DS_%d",NUMBER_OF_NODES,number_of_src_dest,network_set,dataset_num);
      printf("in file.. %s\n",dataset_filnames);
      int temp_topo[NUMBER_OF_NODES][NUMBER_OF_NODES];
      copy_2D_array(NUMBER_OF_NODES,NUMBER_OF_NODES,temp_topo,network_info-&gt;topology);
      FILE* temp_dataset = fopen(dataset_filnames,"w");
      int number_of_source_destination_generated = 0;
      int source_destination[_BIG_SIZE][2];
      for (i = 0; i &lt; NUMBER_OF_NODES; ++i)
      {
          for (j = 0; j &lt; NUMBER_OF_NODES; ++j)
          {
  
              if(i != j)
              {
                  int shortestpath[NUMBER_OF_NODES];
                  int dist = shortest_path(i,j,temp_topo,shortestpath);
                  int paths[K][NUMBER_OF_NODES];
                  initialize_two_d_array(K,NUMBER_OF_NODES,paths,-1);
                  if(dist&gt;=percentage_of(PERCENTAGE_LOWER_LIMIT,compute_optical_reach()) &amp;&amp;
                                          dist&lt;=percentage_of(PERCENTAGE_UPPER_LIMIT,compute_optical_reach()))
                  {
                      int paths_generated = k_shortest_path(i,j,K,
                              paths,temp_topo,compute_optical_reach());
                      if(paths_generated&gt;=K)
                      {
                          source_destination[number_of_source_destination_generated][0] = i;
                          source_destination[number_of_source_destination_generated][1] = j;
                          number_of_source_destination_generated ++;
                      }
  
  
                  }
              }
          }
      }
      if(number_of_source_destination_generated&gt;=NUMBER_OF_SOURCE_DESTINATION)
      {
          int visited_demand[number_of_source_destination_generated];
          initialize_one_d_array(number_of_source_destination_generated,visited_demand,0);
  
          for (i = 0; i &lt; NUMBER_OF_SOURCE_DESTINATION; ++i)
          {
              int next_demand = random_in_range(0,number_of_source_destination_generated);
              while(visited_demand[next_demand])
                  next_demand = random_in_range(0,number_of_source_destination_generated);
              visited_demand[next_demand] = 1;
              fprintf(temp_dataset,"%d\t%d\n",source_destination[next_demand][0],source_destination[next_demand][1]);
          }
      }
  
      else
      {
          printf("Number of Source-Destination Pairs Generated: %d\n",number_of_source_destination_generated);
          printf("Cannot generate the required number of SD pairs\nExiting...");
          exit(1);
      }
  
  
      printf("Number of Source-Destination Pairs Generated: %d\n",number_of_source_destination_generated);
      printf("Freeing up memory and flushing all buffer...\n");
      fclose(temp_dataset);
      printf("Done Generating data-sets.\n");
  }
  </code></pre>
  
  <p>I realized that when I open the file, the data inside the <code>network_info</code> of type <code>NI</code> (a <code>struct</code>) gets corrupted. The <code>network</code> struct has a 2 dimensional array that represents the network. Just after opening the file, i saw garbage values been introduced in the array. The size of the <code>network_info</code> is initialized using <code>(NI*) malloc(sizeof(NI*))</code>. </p>