Segmentation fault when passing Poppler objects



Hi. I get segmentation fault if I try to access a Poppler object whose
pointer is passed through g_signal_connect. There is no such problem
with normal pointers, though.

For example, if I run the following code...
#include <gtk/gtk.h>

struct Colors
{
        double red;
        double green;
        double blue;
};

void draw_cb(GtkWidget *drawing_area, struct Colors *colors)
{
        g_print("%p\n", colors);
        g_print("current colors are:\n");
        g_print("red %f\n", colors->red);
        g_print("green %f\n", colors->green);
        g_print("blue %f\n", colors->blue);
        // draw something
}

void main()
{
        gtk_init(NULL, NULL);

        struct Colors *colors = (struct Colors *)
g_malloc(sizeof(struct Colors));
        g_print("%p\n", colors);

        GtkWidget *window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
        gtk_window_set_default_size(GTK_WINDOW(window), 400, 400);
        g_signal_connect(window, "delete-event",
G_CALLBACK(gtk_main_quit), NULL);

        GtkWidget *drawing_area = gtk_drawing_area_new();
        g_signal_connect(drawing_area, "draw", G_CALLBACK(draw_cb), colors);

        gtk_container_add(GTK_CONTAINER(window), drawing_area);
        gtk_widget_show_all(window);

        gtk_main();

        g_free(colors);
}
...this is the output I get.
0x5571faa9c6e0
0x5571fac68200
current colors are:
red 0.000000
green 0.000000
blue 0.000000
As we can see, colors in main and colors in draw_cb have different
values, but this doesn't stop us from accessing the object (I wonder
how this works, though it's not important in this case).

Now let's consider the same case but using PopplerDocument* instead of
struct Colors*.
#include <gtk/gtk.h>
#include <poppler.h>

void doc_cb(GtkWidget *drawing_area, PopplerDocument *doc)
{
        g_print("%p\n", doc);
        g_print("%d\n", poppler_document_get_n_pages(doc));
}

void main()
{
        gtk_init(NULL, NULL);

        PopplerDocument *doc =
poppler_document_new_from_file("file:///tmp/test/mypdf.pdf", NULL,
NULL);
        g_print("%p\n", doc);

        GtkWidget *window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
        gtk_window_set_default_size(GTK_WINDOW(window), 400, 400);
        g_signal_connect(window, "delete-event", G_CALLBACK(gtk_main_quit), NULL);

        GtkWidget *drawing_area = gtk_drawing_area_new();
        g_signal_connect(drawing_area, "draw", G_CALLBACK(doc_cb), doc);

        gtk_container_add(GTK_CONTAINER(window), drawing_area);
        gtk_widget_show_all(window);

        gtk_main();
}
Output:
0x5584e8573900
0x5584e813d180
Segmentation fault (core dumped)
Here we get segmentation fault, probably because doc in doc_cb is not
mapped to doc in main.

My question is obviously how can I work around this issue and get
valid PopplerDocument object in main? I tried passing &doc as
PopplerDocument** but it did not work.


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