RE: memory allocation in GLIB



I have a problem (SEGFAULT) when freeing a vector allocated using 
routines in GLIB (version 1.2.10).

I declare my variable as:
      gfloat *vector;

Just as a word of advise, it is usually best to initialise pointers to NULL
and check for NULL before you free them, and generally, when I have free'd a
pointer, I set it to NULL afterwards.  This way I should never get into a
situation where I am freeing memory already free'd and I know if it is being
used based on the pointers value.

Just makes me cringe when I see unintialised pointers :S

:)


I then allocate memory as:
      vector = g_malloc ( (some_int)*sizeof(gfloat) );

This allows me to access the memory using:
      for (i=0; i<(some_int); i++)  vector[i] = some_float;

then when exiting the function, I free the memory using:
      g_free( vector );

This statement causes a SEGFAULT.  Can anyone suggest what I am doing 
wrong.

Also, what does a back trace report from gdb?

Regards,
Martyn



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]