Search code examples
cglibgio

g_file_new_for_path () causes segmentation fault


// gcc -o 0 $(pkg-config --cflags --libs gtk+-2.0) 1.c
#include <gtk/gtk.h>
int main (int argc, char *argv[]) {
    GFile *f1 = NULL;
    f1 = g_file_new_for_path ("/home/user/1.txt");
    g_printf ("File loaded successfully.\n");
    return 0;
}

When I run this program it causes segmentation fault at g_file_new_for_path ()(whether or not /home/user/1.txt exists).
Have I miswrote the code? Or is it a bug for my system?

P. S. : My system is Arch Linux, and GLib version is 2.28.8-1.


Solution

  • You need to call g_type_init() before using g_file_new_for_path -- as per this thread. After that, the program works.