Re: (2) Segmetation fault assigning a GdkPixmap* to a GdkPixmap& parameter (using C++)



On Wed, Jun 06, 2001 at 11:16:37PM +0200, Ignacio Nodal wrote:
> Sorry,there was a fault on the function I pasted ;))
> 
> gboolean ParameterKernel::ReadPixmap(gchar **pixstring, GdkPixmap& pix)
> //Reads a pixmap from a xpm-format string & stores it in GdkPixmap& pix
> {
>   
>   GdkPixmap *pixmap;
>   GdkBitmap *mask;
> 
>   pixmap = gdk_pixmap_colormap_create_from_xpm_d (NULL,
> gtk_widget_get_default_colormap(),
>                                                 &mask, NULL, (gchar **)
> pixstring);
> 
> 
>   
>   g_print("The program arrives this point\n");
>   pix =  *pixmap;

&pix must be a valid pointer, or this will segfault, just like if (char *)ptr is not a valid pointer, *ptr = 'f' will segfault.
This means you're doing something wrong if..

>   g_print("Segmentation fault is in the line above\n");
>   
>   if (&pix == NULL) {return (FALSE);} else {return (TRUE);}

..you're expecting &pix might be NULL here. I've forgotten everything I ever knew about c++ but I don't think it can be NULL anyway.

Using GTK structures without any indirection is a bad idea, besides. I suspect what you really wanted to do here is something like this, using a reference to a pointer:

gboolean ParameterKernel::ReadPixmap (gchar **pixstring, GdkPixmap*& pix)
{
    GdkPixmap *pixmap;
    GdkBitmap *mask;

    pixmap = gdk_pixmap_colormap_create_from_xpm_d (NULL,
	gtk_widget_get_default_colormap(), &mask, NULL, pixstring);

    pix = pixmap;

    if (pix == NULL)
	return FALSE;
    else
	return TRUE;
}

Then you would use the function like this:

#include "myxpm.xpm" // declares char *myxpm[]
GdkPixmap *pixmap;
ParameterKernel::ReadPixmap (myxpm, pixmap);





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