l i n u x - u s e r s - g r o u p - o f - d a v i s
Next Meeting:
July 7: Social gathering
Next Installfest:
Latest News:
Jun. 14: June LUGOD meeting cancelled
Page last updated:
2010 Apr 12 16:59

The following is an archive of a post made to our 'vox-tech mailing list' by one of its subscribers.

Report this post as spam:

(Enter your email address)
Re: [vox-tech] Got a memory leak. Ugh!
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

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

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


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.
> }}}
vox-tech mailing list

LUGOD Group on LinkedIn
Sign up for LUGOD event announcements
Your email address:
LUGOD Group on Facebook
'Like' LUGOD on Facebook:

Hosting provided by:
Sunset Systems
Sunset Systems offers preconfigured Linux systems, remote system administration and custom software development.

LUGOD: Linux Users' Group of Davis
PO Box 2082, Davis, CA 95617
Contact Us

LUGOD is a 501(c)7 non-profit organization
based in Davis, California
and serving the Sacramento area.
"Linux" is a trademark of Linus Torvalds.

Sponsored in part by:
Appahost Applications
For a significant contribution towards our projector, and a generous donation to allow us to continue meeting at the Davis Library.