Re: Problem with GList



Yves Kurz wrote:

I'm trying to write a little diary pragram with gtk but now
i have a problem. I get a segfault but i don't know why.
Unfortunately this is my first gtk application, therefore i
don't really know what im doing..
Perhaps you can help me and tell me what's wrong.

(...)

typedef struct {
      char *date_time_string;
      char *topic;
      char *text;
      int i;
      time_t time_t;
} DIARY_TABLE_LINE;


GList *diary_data = NULL;

void 
add_diary_entry (time_t time, char *topic, char *text)
{
      DIARY_TABLE_LINE *diary_entry, *d;
      GList *li = NULL;
      
      diary_entry = g_malloc (sizeof (DIARY_TABLE_LINE));
      
      diary_entry->date_time_string = time_t_to_string (time);
      diary_entry->topic = g_strdup (topic);
      diary_entry->text = g_strdup (text);
      diary_entry->time_t = time;   
      
      g_list_append (diary_data, diary_entry);        

You need to assign the result of g_list_append() to the diary_data
variable:  if the start of the list changes, then since diary_data
must point to the first element of the list, it needs to be changed.
This applies to many of the functions which add or remove elements
from GLists.

So,

        diary_data = g_list_append (diary_data, diary_entry);

      rows++;

/* g_list_lenght(diary_data) returns 0.. but why?? */

/* segfault roughly here*/
      li = g_list_first (diary_data);

The segfault is occuring because diary_data == NULL here, so
g_list_first returns NULL, and you then dereference the NULL pointer.

Jonathan




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