Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

what causes this program to seg fault?

66 views
Skip to first unread message

nvangogh

unread,
Aug 8, 2012, 2:43:17 PM8/8/12
to
#include <X11/Xlib.h>
#include <unistd.h>
#define NIL (0)

int main()
{
// Open the display

Display *dpy = XOpenDisplay(NIL);
assert(dpy);

// Get some colors

int blackColor = BlackPixel(dpy, DefaultScreen(dpy));
int whiteColor = WhitePixel(dpy, DefaultScreen(dpy));

// Create the window

Window w = XCreateSimpleWindow(dpy, DefaultRootWindow(dpy), 0, 0,
200, 100, 0, blackColor, blackColor);

// We want to get MapNotify events

XSelectInput(dpy, w, StructureNotifyMask);

// "Map" the window (that is, make it appear on the screen)

XMapWindow(dpy, w);

// Create a "Graphics Context"

GC gc = XCreateGC(dpy, w, 0, NIL);

// Tell the GC we draw using the white color

XSetForeground(dpy, gc, whiteColor);

// Wait for the MapNotify event

for(;;) {
XEvent e;
XNextEvent(dpy, &e);
if (e.type == MapNotify)
break;
}

// Draw the line

XDrawLine(dpy, w, gc, 10, 60, 180, 20);

// Send the "DrawLine" request to the server

XFlush(dpy);

// Wait for 10 seconds

sleep(10);
}

It compiles without error, but if i run it from the console it will
generate a segmentation fault error.

Keith Thompson

unread,
Aug 8, 2012, 3:03:10 PM8/8/12
to
nvangogh <nvan...@invalid.net> writes:
> #include <X11/Xlib.h>
> #include <unistd.h>
> #define NIL (0)
>
> int main()
> {
> // Open the display
>
> Display *dpy = XOpenDisplay(NIL);
> assert(dpy);
[snip]
> XFlush(dpy);
>
> // Wait for 10 seconds
>
> sleep(10);
> }
>
> It compiles without error, but if i run it from the console it will
> generate a segmentation fault error.

Once I add

#include <assert.h>

it compiles and runs without error for me (tested under Cygwin, Ubuntu,
and Solaris).

assert() is defined as a macro. My best guess is that you have a
function or other symbol with the same name defined somewhere in your
library, and your compiler is permitting the implicit declaration and
calling that function, which is incompatible with the assert() macro.

Try adding "#include <assert.h>" and see if that fixes it.

Incidentally, what is the purpose of

#define NIL (0)

? Why not just use the predefined NULL macro? And in any case, the
parentheses in the definition are unnecessary (but harmless).

--
Keith Thompson (The_Other_Keith) ks...@mib.org <http://www.ghoti.net/~kst>
Will write code for food.
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"

nvangogh

unread,
Aug 8, 2012, 5:59:42 PM8/8/12
to
Hi Keith,
I got it to run under debian but i am having problems getting it to work
on the console / terminal in freebsd. I have been trying to find out if
a window manager is necessary to run this but am not able to find clear
documentation on this point. I am trying to get this to work on a system
that does have x windows configured - but I am not invoking it from
within x windows.
Regards

Ike Naar

unread,
Aug 8, 2012, 6:21:50 PM8/8/12
to
On 2012-08-08, nvangogh <nvan...@invalid.net> wrote:
> I am trying to get this to work on a system
> that does have x windows configured - but I am not invoking it from
> within x windows.

If the DISPLAY environment variable is not pointing to a
working X display,

Display *dpy = XOpenDisplay(NULL);

will not succeed, and dpy will be set to NULL.

Keith Thompson

unread,
Aug 8, 2012, 6:33:14 PM8/8/12
to
Hmm, that's odd. I definitely wrote "#include <assert.h>" both times,
with a space between "#include" and "<", but the quoted material in your
followup shows it as "#include<assert.h>" without the space. Did your
newsreader do that?

Anyway ...

> I got it to run under debian but i am having problems getting it to
> work on the console / terminal in freebsd. I have been trying to find
> out if a window manager is necessary to run this but am not able to
> find clear documentation on this point. I am trying to get this to
> work on a system that does have x windows configured - but I am not
> invoking it from within x windows.

Did you try adding the "#include <assert.h>" as I suggested? Did it
make a difference?

A window manager shouldn't be necessary, as long as you have a usable X
server running. But it would be quite unusual to be running an X server
with no window manager.

And if you don't, the assert() should fail unless you've defined NDEBUG.

But really, assert() should be used to catch programming errors, not
run-time errors. It's a way of saying "This should never happen, but if
it does, tell me about it and give up on doing anything else."

For XOpenDisplay(NULL) to work, you need to have the environment
variable $DISPLAY set to something valid, such as ":0.0". This *should*
be set for you automatically. Try running a simple known-to-work X11
program such as xlogo; this will tell you whether it's a problem with
the X server or a problem in your program.

First see if you can get the assert() to work without causing a
segmentation fault. Then I suggest replacing the assert() with
something like:

if (dpy == NULL) {
fprintf(stderr, "XOpenDisplay failed\n");
exit(EXIT_FAILURE);
}

And what *exactly* does "problems getting it to work" mean? Is this
still the segmentation fault problem?

If your problem is with X11, you might try posting in one of the
comp.windows.x newsgroups (assuming they're still active, I haven't
visited lately).

Keith Thompson

unread,
Aug 8, 2012, 6:34:06 PM8/8/12
to
Which he checks for, but I suspect the check isn't being done correctly.

Vincenzo Mercuri

unread,
Aug 9, 2012, 3:58:42 AM8/9/12
to
Il 08/08/2012 21:03, Keith Thompson ha scritto:
[..]
>
> Try adding "#include <assert.h>" and see if that fixes it.
>
> Incidentally, what is the purpose of
>
> #define NIL (0)
>
> ? Why not just use the predefined NULL macro? And in any case, the
> parentheses in the definition are unnecessary (but harmless).
>

Hi Keith,
I think he's following this tutorial:
http://tronche.com/gui/x/xlib-tutorial/2nd-program-anatomy.html
which does actually add "#include <assert.h>". I got it compile
and run on my Xubuntu 12.04 64-bit, with no problems.

--
Vincenzo Mercuri

Christian Freund

unread,
Aug 28, 2012, 3:15:11 AM8/28/12
to
"Keith Thompson" schrieb im Newsbeitrag
news:lnvcgtu...@nuthaus.mib.org...
> Display *dpy = XOpenDisplay(NULL);
>
> will not succeed, and dpy will be set to NULL.

Which he checks for, but I suspect the check isn't being done correctly.


__

Please "nvangogh" instead of that assert() try out something like:

"if(!dpy) return 0xffff;"

Or finally use a debugger, or print out the return-codes of everything on
stderr.

Keith Thompson

unread,
Aug 28, 2012, 4:51:48 AM8/28/12
to
Christian, quoted text should be marked by prepending "> " to each line.
Your newsreader *should* do this for you.

Returning 0xffff from main() is not a good idea. If you want to do that
kind of error check, you can do `return EXIT_FAILURE;` or
`exit(EXIT_FAILURE);`, both of which require `#include <stdlib.h>`.

Francis Glassborow

unread,
Aug 28, 2012, 5:21:44 AM8/28/12
to
On 28/08/2012 09:51, Keith Thompson wrote:
> "Christian Freund" <fre...@wrz.de> writes:
>> "Keith Thompson" schrieb im Newsbeitrag
>> news:lnvcgtu...@nuthaus.mib.org...
>>> Display *dpy = XOpenDisplay(NULL);
>>>
>>> will not succeed, and dpy will be set to NULL.
>>
>> Which he checks for, but I suspect the check isn't being done correctly.
>>
>>
>> __
>>
>> Please "nvangogh" instead of that assert() try out something like:
>>
>> "if(!dpy) return 0xffff;"
>>
>> Or finally use a debugger, or print out the return-codes of everything on
>> stderr.
>
> Christian, quoted text should be marked by prepending "> " to each line.
> Your newsreader *should* do this for you.
>
I am sure it would if he had not replied below the '__' signature separator.



Keith Thompson

unread,
Aug 28, 2012, 1:02:49 PM8/28/12
to
"__" isn't a signature separator; "-- " is (the trailing space is part
of it).
0 new messages