XPutImage() Seg Fault

Richard Overstreet reoverstreet at charter.net
Mon Jan 26 20:24:42 PST 2015


Hello All,

I have a simple code which I'll paste below which I think should display
a black or blank 512x512 pixel image however it segfaults according to
GDB at XPutImage(). I can't find any documentation as to why this is
occurring and I am hoping someone my have some insight. This is my first
exposure to X so I'm not sure what is going wrong and the documentation
has not been very helpful. Here is the code:

#include <X11/Xlib.h>
#include <X11/Xutil.h>

main(int argc, char *argv[])
{
Display            *Monitor;
Window            ImageWindow;
GC                ImageGC;
XWindowAttributes        Atts;
XImage            *Picture;
int                ROWS,COLS;
unsigned char        *displaydata;

/* ... */

 ROWS = COLS = 512;
 
Monitor=XOpenDisplay(NULL);
if (Monitor == NULL)
  {
  printf("Unable to open graphics display\n");
  exit(0);
  }

ImageWindow=XCreateSimpleWindow(Monitor,RootWindow(Monitor,0),
    50,10,        /* x,y on screen */
    COLS,ROWS,    /* width, height */
    2,         /* border width */
    BlackPixel(Monitor,0),
    WhitePixel(Monitor,0));

ImageGC=XCreateGC(Monitor,ImageWindow,0,NULL);

XMapWindow(Monitor,ImageWindow);
XFlush(Monitor);
while(1)
  {
  XGetWindowAttributes(Monitor,ImageWindow,&Atts);
  if (Atts.map_state == IsViewable /* 2 */)
    break;
  }

 unsigned char *dispalydata;

 displaydata = (unsigned char *)calloc(ROWS*COLS*2, sizeof(unsigned char));

 
 
Picture=XCreateImage(Monitor,DefaultVisual(Monitor,0),
        DefaultDepth(Monitor,0),
        ZPixmap,    /* format */
        0,        /* offset */
             (char*)displaydata,/* the data */
        COLS,ROWS,    /* size of the image data */
        16,        /* pixel quantum (8, 16 or 32) */
        0);        /* bytes per line (0 causes compute) */

XPutImage(Monitor,ImageWindow,ImageGC,Picture,
        0,0,0,0,    /* src x,y and dest x,y offsets */
        COLS,ROWS);    /* size of the image data */

XFlush(Monitor);
sleep(2);
XCloseDisplay(Monitor);
}


Thanks in advance to anyone who can help.

-Richard


More information about the xorg mailing list