Re: memory allocation in GLIB



martyn 2 russell bt com wrote:

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

        It seems to be the latest trend to assume that
memory allocation doesn't fail :/ (not only in glib et al)
personaly (hard headed me) I still encapsulate all
functions with return codes in `if's i.e.

if ((ptr = g_strdup_printf("%s bla bla bla", sting)) != NULL) {
        // use string
        g_free(ptr);
}

I guess in a futile hope to be able to someday do:
./configure --disable-goddamn-abort()-in-g_malloc()-calls

or just because I think it looks right.




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?


================ from other mail ... ====================
That is one of my problems, This particular routine is in a plug-in, 
and I have not figured out how to debug
shared libraries with gdb.  :-(

I figure I need to give gdb a path to the directory, something like ...

        LD_LIBRARY_PATH = '/path_to_directory/lib'

Thanks for the advice.


AFAIK as long as you're executable can find
the symbols it needs; gdb will find them too.

I have had problems single-stepping into shared
libraries in the past though.

you might want to try 

nm shared_library | grep symbol

and add a breakpoint at the address
yielded from nm. (hmm I'm not sure if
you had to add a value to that address).

HTH,
                -Tristan



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