From: "Gaël Deest" <GUtopiste@free.fr>
To: linux-c-programming@vger.kernel.org
Subject: Strange segmentation fault with threads / xlib
Date: Sun, 30 Nov 2003 15:28:16 +0100 [thread overview]
Message-ID: <3FC9FE80.3060101@free.fr> (raw)
I wrote this small program (Yes I know, that is stupid, horrible and
useless, but everybody has to learn :-) :
#include <stdio.h>
#include <pthread.h>
#include <X11/Xlib.h>
#define DisplayName NULL
#define DefaultWindowWidth 640
#define DefaultWindowHeight 480
Display *XDisplay;
Window MainWindow;
GC GraphicsContext;
int Border,Background;
static int quit_handler() {
printf("Window closed manually ! Program terminated..\n");
}
static void XMain(void *parm) {
XEvent Event;
int Quit;
Quit = 0;
XSelectInput(XDisplay, MainWindow, ExposureMask | StructureNotifyMask
| ButtonPressMask);
XDrawLine(XDisplay,MainWindow, GraphicsContext, 0, 0,
DefaultWindowWidth,DefaultWindowHeight);
for(;;) {
XNextEvent(XDisplay, &Event);
switch (Event.type) {
case ButtonPress:
printf("Bouton %i ! (%i ; %i)
\n",Event.xbutton.button,Event.xbutton.x,
Event.xbutton.y);
break;
case Expose:
XDrawLine(XDisplay,MainWindow, GraphicsContext, 0, 0,
DefaultWindowWidth,DefaultWindowHeight);
break;
}
if (Quit) break;
XFlush(XDisplay);
}
}
int main(int argc, char *argv[]) {
pthread_t EventThread;
pthread_attr_t *EventThreadAttr;
XEvent
Event;
/* Here is the problem (!) */
XInitThreads();
XSetIOErrorHandler(quit_handler);
XDisplay = XOpenDisplay(DisplayName);
if (XDisplay==0) {
printf("XOpenDisplay : Error ; exiting 1.\n");
exit(1);
} else {
printf("XOpenDisplay : Success.\n");
}
Border = BlackPixel(XDisplay, XDefaultScreen(XDisplay));
Background = WhitePixel(XDisplay, XDefaultScreen(XDisplay));
MainWindow =
XCreateSimpleWindow(XDisplay,XRootWindow(XDisplay,XDefaultScreen(XDisplay)),
20,20,DefaultWindowWidth,DefaultWindowHeight,2,Border,Background);
if (MainWindow==0) {
printf("XCreateSimpleWindow : Error ; exiting 1.\n");
} else {
printf("XCreateSimpleWindow : Success.\n");
}
XMapWindow(XDisplay,MainWindow);
GraphicsContext = XCreateGC(XDisplay,MainWindow,0,NULL);
XSetForeground(XDisplay, GraphicsContext, Border);
XFlush(XDisplay);
pthread_attr_init(EventThreadAttr);
if (pthread_create(&EventThread,EventThreadAttr,(void*)XMain,NULL) != 0) {
printf("Error Creating XMain thread ! Exiting 1.\n");
exit(1);
}
while(1) {
}
}
Which I compile using the following commandline :
gcc xlibtest.c -o xlibtest -L/usr/X11R6/lib -lpthread -lX11
_This_ DOES work. The program is acting exactly as expected. But you may
have noticed the useless declaration "XEvent Event;" at the beginning of
main(). I tried to remove it, and the program crashes with "Segmentation
fault." Yes, simply by removing a useless local scope variable
declaration. I can't use gdb because then the program receives SIGSEGV
all times. It looks like it's not very good at debugging threaded programs.
I'm using :
- GCC 3.3.2
Configured with: ../gcc-3.3.2/configure --prefix=/usr --enable-shared
--enable-threads=posix --enable-__cxa_atexit --disable-checking
--with-gnu-ld --verbose --target=i486-slackware-linux
--host=i486-slackware-linux
- LD 2.14.90.0.7 20031029 (Sounds like a CVS snapshot ? Probably, I
upgraded it with the development version of my distro. Could it be the
problem ?)
- Linux 2.6.0-test9 (Yes, again a test-version :-/ Am simply fond of it.)
May anyone have a look, and tell me where is the problem in my code, or
if there is a need to make a bug report ?
reply other threads:[~2003-11-30 14:28 UTC|newest]
Thread overview: [no followups] expand[flat|nested] mbox.gz Atom feed
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=3FC9FE80.3060101@free.fr \
--to=gutopiste@free.fr \
--cc=linux-c-programming@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).