1
votes

I am trying to make an example of a simple GTK+ viewer using poppler and cairo which I found at gtkforums.com work. However I am getting a segmentation fault (I use anjuta).

When I use the debugger I get this:

ID:1 File: /usr/lib/i386-linux-gnu/libgdk-x11-2.0.so.0 Line: 0 Function: ?? Address: 0x1d3f16 (dont think that matters anyway)

terminal message while debugging:

Debug Terminal for the process: ------------------------------- &"warning: GDB: Failed to set controlling terminal: Operation not permitted\n"

GLib-GObject-WARNING **: cannot register existing type `GdkWindow'

GLib-GObject-CRITICAL **: g_object_new: assertion `G_TYPE_IS_OBJECT (object_type)' failed

Here is my code:

#include <config.h>
#include <glib/gi18n.h>
#include <stdio.h>
#include <stdlib.h>
#include <glib.h>
#include <gtk/gtk.h>
#include <gdk/gdk.h>
#include <cairo.h>
#include <poppler.h>

/* 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->window);
    poppler_page_render(page, cr);
    cairo_destroy(cr);
    return FALSE;
}

int main(int argc, char* argv[]) {
    GtkWidget* win;
    GError* err = NULL;

    gtk_init(&argc, &argv);

    doc = poppler_document_new_from_file("file:///home/user/test.pdf", NULL, &err);
    if (!doc) {
        printf("%s\n", err->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;
}
2
Can you go through with your debugger and tell us the line where the seg fault occurs?? From this code, there's a few places this could happen.Tony The Lion
Make sure you build your program with debugging information enabled, so that the debugger can properly tell you where it's crashing.unwind
i cant seem to find a way to copy all the debugger messages,but here's the error: error,msg="Cannot access memory at address 0x0"Rrjrjtlokrthjji

2 Answers

1
votes

Based upon your comment it ("Cannot access memory at address 0x0") appears that the program has a function that fails, but you didn't check the function's return value to ensure it is not NULL before using it.

Based purely on the code example above the first case of that would be if gtk_window_new failed; then the g_signal_connect and other functions cannot use the win value sensibly.

When I compiled the snippet, it compiled fine without warnings, and executed correctly. There was a poppler error message about the PDF document, but it wasn't relevant to your problems.

So your problem is likely either elsewhere (not within the included example) or a trivial mistake.

1
votes

One way to get more information is to set a breakpoint on g_log in the debugger and get a backtrace of the warning / critical. With the backtrace you'll be able to see where exactly your code is calling the gtk+ function that is failing.