5frame
5frame

Reputation: 101

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.

Upvotes: 2

Views: 1308

Answers (2)

Andrea Azzarone
Andrea Azzarone

Reputation: 21

Firs of all you should use

// gcc -o 0 $(pkg-config --cflags --libs gio-2.0) 1.c    
#include <gio/gio.h>

instead of

// gcc -o 0 $(pkg-config --cflags --libs gtk+-2.0) 1.c    
#include <gtk/gtk.h>

Then you should g_type_init() before of g_file_new_for_path(...).

Upvotes: 2

michel-slm
michel-slm

Reputation: 9756

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

Upvotes: 3

Related Questions