[comp.windows.x] XtAppMainLoop - are there alternatives?

mbarnett@cs.utexas.edu (Michael Barnett) (12/13/89)

i am writing an X application that is accessed from inside of a
lisp application (using some facilities KCL provides for linking
KCL common lisp functions and C code).

my problem is that i don't want to call XtAppMainLoop as the event
loop. if i do, then when i want to exit the X application and return
to the lisp prompt, the X manual (X Toolkit Intrinsics - C Language
Interface X11 R3) says to call "exit" (from a callback, e.g.), but 
this dumps me out of KCL and back to UNIX. (the manual says to do 
this since XtAppMainLoop never returns.)

so in section 7.6 it says:

	"there is nothing special about XtAppMainLoop; it is simply an
	 infinite loop that calls XtAppNextEvent and then XtDispatchEvent.

	 Applications can provide their own version of this loop, which
	 tests some global termination flag ... before circling back to
	 the call to XtAppNextEvent"

therefore i wrote the following:

XtAppContext app_context;
int NOT_DONE;

void main_loop()
{
  XEvent *event;
  NOT_DONE = 1;
  while (NOT_DONE) {
    XtAppNextEvent(app_context,&event);
    XtDispatchEvent(&event);
  }
}

void quit()
{
  XtDestroyApplicationContext(app_context);
  NOT_DONE = 0;
}

the application works fine, KCL allows me to define a function
main-loop that does nothing but call main_loop in the C code.
but when i try to call quit (from a callback), i get a 
segmentation violation (which occurs in the function main_loop,
after the while loop). on the other hand, if i set NOT_DONE to 
0 before the while loop, then the function returns and i get back 
to the lisp prompt. i know this is rather skimpy on the details, 
but does anyone have any ideas? i would very much appreciate it.

mike barnett (mbarnett@cs.utexas.edu)

swick@ATHENA.MIT.EDU (Ralph R. Swick) (12/13/89)

    Date: 12 Dec 89 16:57:51 GMT
    From: snorkelwacker!usc!cs.utexas.edu!mbarnett@BLOOM-BEACON.MIT.EDU
 (Michael Barnett)

    void quit()
    {
      XtDestroyApplicationContext(app_context);
      NOT_DONE = 0;
    }

    the application works fine, KCL allows me to define a function
    main-loop that does nothing but call main_loop in the C code.
    but when i try to call quit (from a callback), i get a 
    segmentation violation (which occurs in the function main_loop,
    after the while loop).

Don't destroy the application context from within XtDispatchEvent
(i.e. from within a callback).  This destroys lots of state that
Xt needs.  If you simply set your flag and do the destroy when
XtDispatchEvent returns, you'll be safe.