Hate to answer my question, but sometimes finding the answer is the
process of quantifying the question.

I ran it generating and freeing 2500 elements 30000 times. Seems that it
is freeing the memory. See output below.

brian

br...@davostro:~/school/Project/a5$ ./test_array_ptr 
GLib Memory statistics (successful operations):
 blocks of | allocated  | freed      | allocated  | freed      | n_bytes   
  n_bytes  | n_times by | n_times by | n_times by | n_times by | remaining 
           | malloc()   | free()     | realloc()  | realloc()  |           
===========|============|============|============|============|===========
        64 |          0 |          0 |      30000 |      30000 |         +0
       128 |          0 |          0 |      30000 |      30000 |         +0
       256 |          0 |          0 |      30000 |      30000 |         +0
       508 |          3 |          0 |          0 |          0 |      +1524
       512 |          0 |          0 |      30000 |      30000 |         +0
      1024 |          0 |          0 |      30000 |      30000 |         +0
      2040 |          1 |          0 |          0 |          0 |      +2040
      2048 |          0 |          0 |      30000 |      30000 |         +0
      2500 |          1 |          0 |          0 |          0 |      +2500
   >  4096 |          0 |      30000 |      90000 |      60000 |        ***
GLib Memory statistics (failing operations):
 --- none ---
Total bytes: allocated=981126064, zero-initialized=6064 (0.00%), 
freed=981120000 (100.00%), remaining=6064

#include <glib.h>
#include "types.h"
#include "rand.h"
#include "print.h"

void mydestroy(gpointer myInd) {
  g_slice_free1(sizeof(individual), myInd);
  //g_free(myInd);
}

int main() {
  GPtrArray *myAry;
  GRand *myRand;
  individual *theInd;
  guint i,j;

  g_mem_set_vtable(glib_mem_profiler_table);
  g_atexit(g_mem_profile);

  // Initialize the random number generator
  myRand = g_rand_new();

  // Do this thing 30,000 times. Give it a workout!
  for (j=0; j < 30000; j++) {
    myAry = g_ptr_array_new_with_free_func(mydestroy);
    
    // Add lots of individuals to the array.
    for (i=0; i<2500;i++) {
      makeRandIndV1(myRand, &theInd);
      g_ptr_array_add(myAry, theInd); 
    }

    // free the array.
    g_ptr_array_free(myAry, TRUE);
  }


  return 0;
}
On Sun, Mar 07, 2010 at 12:08:11PM -0800, Brian Lavender wrote:
> I am attempting to create a g_ptr_array_new_with_free_func. It appears
> that the memory is not being free after calling 
> g_ptr_array_free(myAry, TRUE);
> 
> Shouldn't it release the memory for all the elements?
> 
> 
> GLib Memory statistics (successful operations):
>  blocks of | allocated  | freed      | allocated  | freed      | n_bytes   
>   n_bytes  | n_times by | n_times by | n_times by | n_times by | remaining 
>            | malloc()   | free()     | realloc()  | realloc()  |           
> ===========|============|============|============|============|===========
>         64 |          0 |          1 |          1 |          0 |         +0
>        508 |          3 |          0 |          0 |          0 |      +1524
>       2040 |          1 |          0 |          0 |          0 |      +2040
>       2500 |          1 |          0 |          0 |          0 |      +2500
> GLib Memory statistics (failing operations):
>  --- none ---
> Total bytes: allocated=6128, zero-initialized=6064 (98.96%), freed=64 
> (1.04%), remaining=6064
> 
> 
> #include <glib.h>
> #include "types.h"
> #include "rand.h"
> #include "print.h"
> 
> void mydestroy(gpointer myInd) {
>   g_slice_free1(sizeof(individual), myInd);
>   //g_free(myInd);
> }
> 
> int main() {
>   GPtrArray *myAry;
>   GRand *myRand;
>   individual *theInd;
>   guint i;
> 
>   g_mem_set_vtable(glib_mem_profiler_table);
>   g_atexit(g_mem_profile);
> 
>   myAry = g_ptr_array_new_with_free_func(mydestroy);
> 
>   // Initialize the random number generator
>   myRand = g_rand_new();
> 
>   // Add some individuals to the array.
>   for (i=0; i<5;i++) {
>     makeRandIndV1(myRand, &theInd);
>     g_ptr_array_add(myAry, theInd); 
>   }
> 
>   // print them out
>   for (i=0; i<5;i++) {
>     theInd = g_ptr_array_index(myAry, i);
>     print_individual(theInd );
>   }
> 
>   // free the array.
>   g_ptr_array_free(myAry, TRUE);
> 
>   
> 
>   return 0;
> }
> 
> -- 
> Brian Lavender
> http://www.brie.com/brian/
> 
> "There are two ways of constructing a software design. One way is to
> make it so simple that there are obviously no deficiencies. And the other
> way is to make it so complicated that there are no obvious deficiencies."
> 
> Professor C. A. R. Hoare
> The 1980 Turing award lecture
> _______________________________________________
> gtk-app-devel-list mailing list
> gtk-app-devel-list@gnome.org
> http://mail.gnome.org/mailman/listinfo/gtk-app-devel-list

-- 
Brian Lavender
http://www.brie.com/brian/

"About 3 million computers get sold every year in China, but people don't
pay for the software. Someday they will, though. As long as they are going
to steal it, we want them to steal ours. They'll get sort of addicted, and
then we'll somehow figure out how to collect sometime in the next decade."

-- Bill Gates (Microsoft) 1998
_______________________________________________
gtk-app-devel-list mailing list
gtk-app-devel-list@gnome.org
http://mail.gnome.org/mailman/listinfo/gtk-app-devel-list

Reply via email to