Gio & memory management



Hello,

I wrote a program that list recursively files from a given directory.
I'm impressed by the memory usage of such a program (after one minutes,
it uses more than 100 BM on my /usr/ directory... after 5 minute I have to kill the process : I've only 512 MB of RAM... so my system is becoming unstable).
(for comparison, the same program written in php uses less than 1 MB to
list all files of my /usr/).

Is something wrong in my code? Do I forgot to unref or free something?

Regards,
Nicolas

#include <glib.h>
#include <gio/gio.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>


void scan_dir(char const *path) {
        GFile *file;
        file = g_file_new_for_path(path);
        GFileQueryInfoFlags flags;

        GError *error;
        error = g_error_new(g_quark_from_static_string("TEST_ERROR"), 0, "Error");

        GFileEnumerator *enumerator;
        enumerator = g_file_enumerate_children(file, "*", flags, NULL, &error);
        if (enumerator != NULL) {
                GFileInfo *file_info;
                while ((file_info = g_file_enumerator_next_file(enumerator, NULL, &error)) != NULL) {

                        const char *filename;
                        filename = g_file_info_get_name(file_info);

                        GFileType file_type;
                        file_type = g_file_info_get_file_type(file_info);

                        if (file_type == G_FILE_TYPE_DIRECTORY && !g_file_info_get_is_symlink(file_info)) {
                                char *child_path;
                                child_path = (char*) malloc((strlen(path) + strlen(filename) + 1 + 10) * 
sizeof(char));
                                strcpy(child_path, path);
                                strcat(child_path, filename);
                                strcat(child_path, "/");

                                printf("ENTER IN %s\n", child_path);
                                scan_dir(child_path);
                                printf("EXIT FROM %s\n", child_path);

                                free(child_path);
                        }
                        else {                  
                                printf("%s\n", filename);
                        }
                }
        
                if (g_file_enumerator_close(enumerator, NULL, NULL) == FALSE)
                        fprintf(stderr, "Can not close enumerator\n");

g_object_unref(enumerator); g_free(file_info); } g_object_unref(file); g_error_free(error);
}



int main(int argc, char** argv) {
        g_type_init();

        if (argc != 2)
                return 0;

        scan_dir(argv[1]);
        
        return 0;
}





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