#XCreateSimpleWindow causing segfault

25 messages · Page 1 of 1 (latest)

faint sluice
#

Does anyone know why this is causing a segfault?

#include <X11/Xlib.h>

int main() {
    Display *display = XOpenDisplay(NULL);
    Window w = XCreateSimpleWindow(display, DefaultRootWindow(display), 0, 0, 400, 400, 1, 0, 0);
}
tulip solar
#

also needs to call xclosedisplay

faint sluice
tulip solar
#

What's your DISPLAY env var?

#

@faint sluice

faint sluice
tulip solar
#

Your environment variable called DISPLAY

tulip solar
#

Display *XOpenDisplay(char *display_name);
display_name Specifies the hardware display name, which determines the display and communications domain to be used. On a POSIX-conformant system, if the display_name is NULL, it defaults to the value of the DISPLAY environment variable.

-> The env var is not set in your case -> It defaults to the empty string -> That display doesn't exist -> XOpenDisplay returns NULL

https://www.x.org/releases/X11R7.5/doc/man/man3/XOpenDisplay.3.html

#

Sounds like ChatGPT code

faint sluice
tulip solar
#

Passing NULL into XOpenDisplay is your issue

faint sluice
tulip solar
#

Probably because of a change in your environment variables?

faint sluice
#

how can I set that environment variable, and how can I figure out what to set it to?

tulip solar
#

If you're running one instance of X server then the display name is probably :0

faint sluice
#

ok, found that it is indeed :0

tulip solar
#

You could just pass in ":0" into XOpenDisplay as well

faint sluice
#

that still segfaults, so it's probably some other env var that's causing the problem?

tulip solar
#

Try printing out the return value

faint sluice
#

it prints 0

#

(passing ":0" into it)