title: Segmentation fault at GTK+ program
body: <p>I am trying to make an example of a simple <strong>GTK+ viewer</strong> using poppler and cairo which I found at <a href="http://gtkforums.com" rel="nofollow">gtkforums.com</a> work. However I am getting a segmentation fault (I use <strong>anjuta</strong>). </p>
  
  <p>When I use the debugger I get this:</p>
  
  <p><strong>ID:1
  File: /usr/lib/i386-linux-gnu/libgdk-x11-2.0.so.0
  Line: 0
  Function: ??
  Address: 0x1d3f16 (dont think that matters anyway)</strong></p>
  
  <p>terminal message while debugging:</p>
  
  <blockquote>
    <p>Debug Terminal for the process:
    ------------------------------- &amp;"warning: GDB: Failed to set controlling terminal: Operation not permitted\n"</p>
    
    <p>GLib-GObject-WARNING **: cannot register existing type `GdkWindow'</p>
    
    <p>GLib-GObject-CRITICAL **: g_object_new: assertion `G_TYPE_IS_OBJECT
    (object_type)' failed</p>
  </blockquote>
  
  <p>Here is my code: </p>
  
  <pre><code>#include &lt;config.h&gt;
  #include &lt;glib/gi18n.h&gt;
  #include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;glib.h&gt;
  #include &lt;gtk/gtk.h&gt;
  #include &lt;gdk/gdk.h&gt;
  #include &lt;cairo.h&gt;
  #include &lt;poppler.h&gt;
  
  /* gcc `pkg-config --cflags --libs gtk+-2.0 poppler-glib` -o pdfviewer pdfviewer.c */
  
  static PopplerDocument* doc;
  static PopplerPage* page;
  
  static void
  on_destroy(GtkWidget* w, gpointer data) {
      gtk_main_quit();
  }
  
  static gboolean
  on_expose(GtkWidget* w, GdkEventExpose* e, gpointer data) {
      cairo_t* cr;
      cr = gdk_cairo_create(w-&gt;window);
      poppler_page_render(page, cr);
      cairo_destroy(cr);
      return FALSE;
  }
  
  int main(int argc, char* argv[]) {
      GtkWidget* win;
      GError* err = NULL;
  
      gtk_init(&amp;argc, &amp;argv);
  
      doc = poppler_document_new_from_file("file:///home/user/test.pdf", NULL, &amp;err);
      if (!doc) {
          printf("%s\n", err-&gt;message);
          g_object_unref(err);
          return 2;
      }
  
      page = poppler_document_get_page(doc, 0);
      if(!page) {
          printf("Could not open first page of document\n");
          g_object_unref(doc);
          return 3;
      }
  
      int pages = poppler_document_get_n_pages(doc);
      printf("There are %d pages in this pdf.\n", pages);
  
      win = gtk_window_new(GTK_WINDOW_TOPLEVEL);
      g_signal_connect(G_OBJECT(win), "destroy",      G_CALLBACK(on_destroy), NULL);
      g_signal_connect(G_OBJECT(win), "expose-event", G_CALLBACK(on_expose), NULL);
      gtk_widget_set_app_paintable(win, TRUE);
      gtk_widget_show_all(win);
  
      gtk_main();
  
      g_object_unref(page);
      g_object_unref(doc);
  
      return 0;
  }
  </code></pre>