[vox-tech] Got a memory leak. Ugh!

Boris Jeremic jeremic at ucdavis.edu
Sat Apr 10 22:25:59 PDT 2010


run your program under valgrind! It will tell you where you leak...

Boris


Brian Lavender wrote:
> So, I am working on my MS Project. It's a genetic algorithm to train
> rules to identify network attacks. This was supposed to be an easy
> project!!!
> 
> Unfortunately, it leaks memory, which is not such a good thing especially
> when you want to run something over perhaps thousands of iterations. Well,
> I guess 1 byte times 1 thousand is 1 kilobyte. The problem seems to
> occur when I copy pointers to three random individuals into a temporary
> array and grab the two strongest individuals from that array. I believe
> the leak occurs between the ==AAA== markings in the code.  I know most
> people don't want to go pouring through a bunch of tangled logic, so I
> tried to isolate the problem.
> 
> Any ideas why it leaks? Or, any suggestions? 
> 
> brian
> 
> {{{
> #include <glib.h>
> 
> // Put in the memory tracking code here. Only tells at exit though!
> 
> g_mem_set_vtable(glib_mem_profiler_table);
> g_atexit(g_mem_profile);
> 
> // I have an array that has a thousand items in it. I am three random elements into 
> a temporary array. All the original elements are already in 
> 
> // This is the source population array.
> popSrc = g_ptr_array_new_with_free_func(destroyInd);
> 
> for (i=0; i<1000; i++) {
>   trainer = makeRandInd();
>   g_ptr_array_add(popSrc, trainer);
> }
> 
> // Now I create a destination population array.
> popDest = g_ptr_array_new_with_free_func(destroyInd);
> 
> 
> // Loop that does the genetic algorithm
> // Start loop
> 
>      // pick 3 random individuals from the source. and take the strongest two.
>     // Memory leak occurs between starting here ==AAA==
>      threeRand = g_ptr_array_new();
>      for (i=0; i< 3 ;i++) {
> 	j = g_random_int_range(0, nPop);
> 	trainer = g_ptr_array_index(popSrc, j);
> 	g_ptr_array_add(threeRand, trainer);
>       }
> 
>       g_ptr_array_sort(threeRand, sort_function);
> 
>       // fitness goes lowest to highest (0,1,2). Thus elements 1 and 2
>       parent1 = g_ptr_array_index(threeRand, 1);
>       parent2 = g_ptr_array_index(threeRand, 2);
> 
>       g_ptr_array_free(threeRand, FALSE);
>       // And Memory leak suspect are here ===AAA==
> 
>      // crossover
>      // mutate
>      // Add two new children to destination array
> // Repeat loop until new population created.
> }}}


More information about the vox-tech mailing list