Skip to content

fileenumerator.hg: Remove refreturn to avoid memory leak

g_file_enumerator_get_child() is documented as transfer: full. In practice I've found the refreturn does lead to leaks. Example program below.

#include <giomm/init.h>
#include <giomm/file.h>
#include <glibmm.h>
static void weak_notify(gpointer data, GObject *obj) {
    (void)obj;
    bool *b = static_cast<bool*>(data);
    *b = true;
}

int main(int argc, char *argv[]) {
    Gio::init();
    bool child_freed = false;
    {
        auto file = Gio::File::create_for_path(Glib::get_current_dir());
        auto e = file->enumerate_children();
        auto c = e->get_child(e->next_file());
        g_object_weak_ref(G_OBJECT(c->gobj()), &weak_notify, &child_freed);
    }
    if (child_freed) return 0;
    return 1;
}
Edited by Andrew Potter

Merge request reports