Segmentation fault CanvasImage
@JohnCC330
Submitted by JohnCC330 Link to original bug (#782768)
Description
As usual, this might not be a problem of GooCanvas, but I got lost in the debugging and need some indications.
In a Python program I decode a stream from a computer to a label printer. The stream contains PCX images, which I extract, convert to a GdkPixbuf and (try to) draw on a Canvas. Each time a ticket is generated, I create a new Canvas and draw the Pixbuf on it.
The stream I am testing contains two - identical - PCX images. They are extracted correctly, and converted to a Pixbuf.
The problem: The first PCX is converted to pixbuf, and added as a CanvasImage to the first canvas. The second PCX is converted to pixbuf, but, at the moment of the addition to its canvas, I get a segmentation fault.
I'm not an expert programmer, but I followed the execution into the GooCanvas module using gdb, in particular to goocanvasutils.c, line 133, where the segfault occurs. 'p' is pointing to invalid memory. I checked the variables at the start of the function, and they all seem correct (eg width, height, etc) except guchar *gdk_pixels, which is also pointing to invalid memory.
cairo_surface_t* goo_canvas_cairo_surface_from_pixbuf (GdkPixbuf *pixbuf) { gint width = gdk_pixbuf_get_width (pixbuf); gint height = gdk_pixbuf_get_height (pixbuf); guchar *gdk_pixels = gdk_pixbuf_get_pixels (pixbuf); <----- INCORRECT int gdk_rowstride = gdk_pixbuf_get_rowstride (pixbuf); int n_channels = gdk_pixbuf_get_n_channels (pixbuf); guchar *cairo_pixels; cairo_format_t format; cairo_surface_t *surface; static const cairo_user_data_key_t key; int j;
if (n_channels == 3) format = CAIRO_FORMAT_RGB24; else format = CAIRO_FORMAT_ARGB32;
cairo_pixels = g_malloc (4 * width * height); surface = cairo_image_surface_create_for_data ((unsigned char *)cairo_pixels, format, width, height, 4 * width); cairo_surface_set_user_data (surface, &key, cairo_pixels, (cairo_destroy_func_t)g_free);
for (j = height; j; j--) { guchar *p = gdk_pixels; guchar *q = cairo_pixels;
if (n_channels == 3)
{
guchar *end = p + 3 * width;
while (p < end)
{
#if G_BYTE_ORDER == G_LITTLE_ENDIAN
q[0] = p[2]; <----------- Segfaults here
q[1] = p[1];
q[2] = p[0];
#else
q[1] = p[0];
q[2] = p[1];
q[3] = p[2];
#endif
p += 3;
q += 4;
}
I can't imagine how this happens. Both PCXs are identical, and both are converted by the same function to Pixbufs.
I have a faint suspicion this might have something to do with Python's reference counting,
Any suggestion/help as to how to proceed?
Version: 2.0.x