Re: Segmentation fault when passing Poppler objects



On Tue, 6 Nov 2018 at 09:55, Радомир Хаџић via gtk-app-devel-list <
gtk-app-devel-list gnome org> wrote:

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.


This:


void draw_cb(GtkWidget *drawing_area, struct Colors *colors)


Is *not* the signature for a GtkWidget::draw signal callback.

The signature is:

  gboolean (* draw) (GtkWidget *widget, cairo_t *context, gpointer
user_data);


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).


It "works" because you're getting passed a pointer to a memory area, and
you're trying to access it by 3 `sizeof(double)` offsets; the cairo_t
structure is large enough to accommodate those accesses without generating
a segmentation fault, but of course cairo_t does not contain 3 doubles at
the very beginning of its structure, so you're getting garbage that C
helpfully translates to a double representation. I also assume that you're
running on a 64 bit architecture, because if you tried the same of 32 bits
archs, you'd very much get a segmentation fault.

Of course, this will never work for a PopplerDocument instance, because
you're not trying to access the first `3 * sizeof(double)` bytes of it:
you're calling a Poppler function, which expects a PopplerDocument
instance, instead of a cairo_t one:

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


Which is why you're getting a segmentation fault.

I strongly encourage you to read the GTK API reference:

https://developer.gnome.org/gtk3/stable/GtkWidget.html#GtkWidget-draw

In general, you should *always* read the documentation for each signal
you're using, to know the signature of the callback associated to the
signal. The signal machinery disables a lot of the type safety at compile
time in order to allow generic functions to be invoked without ad hoc
emitters.

Ciao,
 Emmanuele.

-- 
https://www.bassi.io
[@] ebassi [@gmail.com]


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