Properly delete GList and identifying the mount point drive (follow-up)

Hi, ALL,
I have 2 follow-up questions on the other thread here: Difference between volume and drive - #34 by oneeyeman1.

  1. From the get_mounts() documentation:

The returned list should be freed with g_list_free(), after its
elements have been unreffed with g_object_unref()

    for( volumes = volumelist; volumes != NULL; volumes = volumes->next )
        g_object_unref( volumes );
    g_list_free( volumelist );

I presume the code adobe is wrong as it throws an exception
on the g_object_unref().

But I can’t just call g_list_free(), right?

I also tried this:

if( volumelist )
{
    g_list_foreach( volumes, g_object_unref, NULL );
    g_list_free( volumelist );
}

which resulted in

error: invalid conversion from ‘void ()(gpointer)’ {aka ‘void
(
)(void*)’} to ‘GFunc’ {aka ‘void ()(void, void*)’} [-fpermissive]
66 | g_list_foreach( volumes, g_object_unref, nullptr );
| ^~~~~~~~~~~~~~
| |
| void ()(gpointer) {aka void
(
)(void*)}
In file included from /usr/include/glib-2.0/glib/ghash.h:36,
from /usr/include/glib-2.0/glib.h:52,
from /usr/include/glib-2.0/gobject/gbinding.h:30,
from /usr/include/glib-2.0/glib-object.h:24,
from /usr/include/glib-2.0/gio/gioenums.h:30,
from /usr/include/glib-2.0/gio/giotypes.h:30,
from /usr/include/glib-2.0/gio/gio.h:28,
/usr/include/glib-2.0/glib/glist.h:139:60: note: initializing
argument 2 of ‘void g_list_foreach(GList*, GFunc, gpointer)’
139 | GFunc func,
| ~~~~~~~~~~~~~~~~^~
make: *** [Makefile:28049: coredll_volumeenum.o] Error 1

  1. How do I uniquely identify the mounted volume?

    auto monitor = g_volume_monitor_get();
    GList *volumes, *volumelist;
    // On some systems, partition is mounted hidden by default
    // If “volumelist” comes back as “nullptr”, it is normal behavior
    if( flagsSet & wxFS_VOL_MOUNTED && !( flagsUnset & wxFS_VOL_MOUNTED ) )
    {
    volumelist = g_volume_monitor_get_mounts( monitor );
    for( volumes = volumelist; volumes != NULL; volumes = volumes->next )
    {
    gpointer volume = volumes->data;
    char *name = g_mount_get_name( (GMount *) volume );
    // store the name in some containter
    g_free( name );
    }
    }

doing this will return a very generic name “XXXGB hard drive”.

And so when I later on loop thru the container, I will not be able to
identify which mount/volume I’m looking at/

Will I be able to do something like this:

GFile *file = g_mount_get_root( (GMount *) volume );
g_file_get_basename( file );

?

Thank you.

Firstly, please give threads a descriptive title. Nobody except the couple of us who have been following along on the previous thread will know what “Follow-up questions” means. You’re narrowing your audience by doing so, and that means others won’t be able to find the questions and their answers in future.


You want to use g_object_unref (volumes->data) because the data in a linked list element (a GList) is stored in the data member.

This is a pretty simple use of a linked list. Please read the GList documentation to find out how linked lists work in GLib before starting to use them.


You should be passing volumelist rather than volumes to g_list_foreach(), and g_object_unref needs to be cast to GFunc to be passed to g_list_foreach(). Modern C requires explicit casts for function pointers, it won’t add an implicit cast for you.


That entirely depends on what use case you have, I can’t really answer that for you. Look through the methods of GVolume/GMount/GDrive and try them until you find something which you’re happy to use as an identifier.

ottomh methods like g_unix_mount_guess_name() are what’s used by Nautilus. If you want to know for sure, please look at the code of whatever app you think names things nicely, and copy from that.

This topic was automatically closed 45 days after the last reply. New replies are no longer allowed.