newbie and g_list*



I am running on linux.  I use scandir to look at a directory.  I do this
periodically so that my GUI can update it's display when new data arrives.
The scandir function mallocs memory for data and I add that to a list.  This
code looks something like this (error checking removed):

    // This works fine.  It's here for background.
    int n;
    struct dirent **dirEntries = NULL;
    n = scandir (directory, &dirEntries, 0, alphasort);
    while (n)
    {
        list = g_list_append (list, dirEntries[n]->d_name);
    }
    free (dirEntries);

The free above does not remove the names, just the structure pointing to
them.  It is malloced by scandir as are the arrays for the file names.  Note
that I did not free the memory for those names.

Later, I want to remove one of them, so I locate it with function f().  And
try

    // This works.
    GList *tmp = f ();
    list = g_list_remove (list, tmp->data);

    // This creates a segmentation fault and I don't understand why.
    free (tmp->data);

I am led to believe that data is just a pointer.  I expect that it contains
the pointer to the directory entry that I assigned to it in the
g_list_append function.  Thus, I can nuke it whenever I want as long as I
have already removed it from the list.  Is that correct?

Please ignore the ignorance.  I've searched the archive and found several
discussions, but I have not learned from there.

Thanks,
Mike





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