[comp.windows.x] XtAppCreateShell. What am I doing wrong?

xg00@GTE.COM (Xev Gittler) (05/26/89)

Could someone perhaps tell me what I am doing wrong? Reading the
documentation on XtAppCreateShell, this seems to be what I am supposed
to do, but it is not working. I keep getting an 
X Toolkit Error: Cannot perform malloc

The following is the code. It gets the error on the XtAppCreateShell
command. If anyone can help me out, I would REALLY appreciate it!

					Xev Gittler
					xg00@gte.com, or
					xg00%gte.com@relay.cs.net


#include <X11/Xlib.h>
#include <X11/Intrinsic.h>
#include <X11/StringDefs.h>
#include <X11/Command.h>

main(argc,argv)
int argc;
char *argv[];

{

XtAppContext    MyApplicationContext;
Widget TopLevel, mybutton;
Display *TheXDisplay;
int n;
Arg args[20];

     XtToolkitInitialize();
     MyApplicationContext = XtCreateApplicationContext();
     TheXDisplay = XtOpenDisplay (MyApplicationContext, NULL, "Xtest",  
				  "Xtest", NULL, 0, &argc, argv);
     TopLevel = XtAppCreateShell (NULL, "Xtest", 
				   "applicationShellWidgetClass", TheXDisplay, 
				  NULL, 0);
     n = 0; 
     XtSetArg (args[n], XtNwidth, 200); n++; 
     XtSetArg (args[n], XtNlabel, "Blah!!"); n++;
     mybutton = XtCreateManagedWidget ("TopLabel", commandWidgetClass, 
					  TopLevel, args, n);

     XtRealizeWidget (TopLevel);
     XtMainLoop();
}

-- 
					Xev Gittler
					xg00@gte.com, or
					xg00%gte.com@relay.cs.net

converse@EXPO.LCS.MIT.EDU (Donna Converse) (05/26/89)

> Could someone perhaps tell me what I am doing wrong? 
> I keep getting an 
> X Toolkit Error: Cannot perform malloc

The error message doesn't really tell you anything, I agree.

1) You don't need to include X11/Xlib.h, but you must include X11/Shell.h

2) In the call to XtAppCreateShell, the widget class parameter should not
   be quoted.  (Maybe you tried quoting it after you got an error because
   Shell.h was not included; it is defined in Shell.h)  Generally, for
   every class of widget that you instantiate, there is a required 
   include file.

These first two caused the error message.  Finally, to get it flying:

3) Replace XtMainLoop() with XtAppMainLoop(MyApplicationContext)
   

Donna Converse
MIT X Consortium

kit@EXPO.LCS.MIT.EDU (Chris D. Peterson) (05/26/89)

Donna's reply covered the problem I think, but there is another thing
or two in here just waiting to bite you...

XtOpenDisplay() can return NULL if it is not able to open the display, if you
try to pass NULL to XtAppCreateShell() it will probabally seg. fault.  You
need to check this value to make sure that a non-NULL display is returned.

Also, it is usually best to pass NULL as the name of the application, this
will cause the toolkit to look for resources using the last component of 
argv[0] as the application name.  This is generally the desired behavior.


                                                Chris D. Peterson
                                                MIT X Consortium

Net:     kit@expo.lcs.mit.edu
Phone:   (617) 253 - 9608
Address: MIT - Room NE43-213

smithrd@inteloa.intel.com (Randy D. Smith) (06/03/89)

In article <8905261610.AA17950@expo.lcs.mit.edu> kit@EXPO.LCS.MIT.EDU (Chris D. Peterson) writes:
>
>Donna's reply covered the problem I think, but there is another thing
>or two in here just waiting to bite you...
>
>                                                Chris D. Peterson
>                                                MIT X Consortium

Speaking of being bitten by the "Toolkit: cannot malloc" bug...

I notice that Xlib has code surrounded by #ifdefs on the preprocessor
variable "MALLOC_0_RETURNS_NULL".  Very nice for SVIDish systems.  I
also noticed (via the aforementioned error message) that the toolkit
was not quite so, how shall we put it, portable (sigh, I know the
word is a bit overworked, but it was the best I could do with such
little notice).

It would be really nice (pretty please?) if the toolkit (and anything
else which becomes a part of "the standard") were at least as portable
as Xlib.  Perhaps for R4?

I have no idea whether this could be the problem the original poster
was facing, but if so...just go into Xt/Alloc.c and make sure all the
allocation routines check for requests for 0 bytes, and pass on to
the libc equivalents requests for 1 (or more) bytes instead.
--
					Randy D. Smith
			 BiiN (tm),  An Information Systems Company
			     ...uunet!tektronix!inteloa!smithrd
					(503) 696-4660

jlf@earth.cray.COM (John Freeman) (06/05/89)

> I notice that Xlib has code surrounded by #ifdefs on the preprocessor
> variable "MALLOC_0_RETURNS_NULL".  Very nice for SVIDish systems.  I
> also noticed (via the aforementioned error message) that the toolkit
> was not quite so, 
> 
> I have no idea whether this could be the problem the original poster
> was facing, but if so...just go into Xt/Alloc.c and make sure all the
> allocation routines check for requests for 0 bytes, and pass on to
> the libc equivalents requests for 1 (or more) bytes instead.

The port of the toolkit by Cray Research does exactly what you
suggest in Xt/Alloc.c.  Here are the diffs:

-------------------------------------------------------------------------------
diff -c ~/X11R3/lib/Xt/Alloc.c .
*** /usr/earth3/jlf/X11R3/lib/Xt/Alloc.c        Tue Sep  6 15:26:46 1988
--- ./Alloc.c   Mon Dec 19 17:37:54 1988
***************
*** 41,46 ****
--- 41,47 ----
      unsigned size;
  {
      char *ptr;
+     if (size == 0) size = 1;
      if ((ptr = malloc(size)) == NULL)
          XtErrorMsg("allocError","malloc","XtToolkitError",
                   "Cannot perform malloc", (String *)NULL, (Cardinal *)NULL);
***************
*** 51,56 ****
--- 52,58 ----
      char     *ptr;
      unsigned size;
  {
+    if (size == 0) size = 1;
     if (ptr == NULL) return(XtMalloc(size));
     else if ((ptr = realloc(ptr, size)) == NULL)
              XtErrorMsg("allocError","realloc","XtToolkitError",
***************
*** 62,67 ****
--- 64,73 ----
      unsigned num, size;
  {
      char *ptr;
+     if ((size == 0) || (num == 0)) {
+       size = 1;
+       num = 1;
+     }
      if ((ptr = calloc(num, size)) == NULL)
           XtErrorMsg("allocError","calloc","XtToolkitError",
                  "Cannot perform calloc", (String *)NULL, (Cardinal *)NULL);