Search code examples
c++linuxsegmentation-faultxlibx11

Segmentation Fault In My Xlib Wrapper Class (C++)


I am attempting to create a wrapper class to the Xlib library, but I get a segmentation fault! I am new to C++ and this is probably out of my depth and maybe have my goals set to high, other than that problem, can someone tell my why I'm getting this segmentation fault?
source file
header file
main.cpp

I believe this is the back trace I get:

Program received signal SIGSEGV, Segmentation fault.
In XMapWindow () (/usr/lib/libX11.so.6)
At /home/elliot/Programming/C and C++/XWindows/src/MyWindow.cpp:49

Solution

  • Your error indicates that either the display or window parameters to XMapWindow() were incorrect.

    You need to include more error checking in your code, in particular for the results of the calls to XOpenDisplay and XCreateWindow.

    The only obvious error I can see is that you're passing the CWBackPixel flag to XCreateWindow but have left the attributes parameter uninitialised. Unlike plain C, C++ does not clear the memory contents of structures when they're declared.

    EDIT - I of course missed the really blindingly obvious error - you've inadvertently redeclared all of your classes member variables within your constructor. That'll be your scope problem. You need to remove the typenames from all of the assignments within the constructor, e.g.:

    MyWindow::MyWindow()
    {   //ctor
        display = XOpenDisplay(NULL);
        visual = XDefaultVisual(display, 0);
        depth = XDefaultDepth(display, 0);
        window = XCreateWindow(display, XRootWindow(display, 0), 0, 0, MyWindow::default_width, MyWindow::default_height, 16, depth, InputOutput, visual, CWBackPixel, &attributes);
        XStoreName(display, window, MyWindow::default_caption.c_str());
        XSelectInput(display, window, ExposureMask | KeyPressMask | KeyReleaseMask);
    }