From: waider@glas.rtsg.mot.com (Ronan Waide)
Subject: XmStrings seem to cause memory leak.....
Organization: Motorola Ireland Ltd., Cork, Ireland.
Date: Tue, 21 Jul 1992 15:03:02 GMT

This is most likely to be a problem with my code, but here goes:

I have an application, mostly built from XDesigner with bits of code tagged on
to do the messy stuff. There's a main window with a panel of buttons on one
side, and a sub window of this, also designated a primary window, so you can
have whichever one you find more interesting on top. The subwindow has a row
of buttons along the bottom. Now, here's the problem: One button on the main
window is a short-cut for a button on the subwindow. They both call the same
routine. The main-window button is labelled "Close", the subwindow button is
labelled "Close" or "Accept" - this is switched elsewhere in the program. The
callback routine is listed below. It crashes at line 59 of the code which is
the call to XmStringCreate. Using malloc_debug to track the problem results in
the following (gdb) stack dump:

----------------
malloc_verify: bad block size (1633841013) at 0x1e2584


Program received signal 6, Abort
0xc8844 in _kill ()
(gdb) where
#0  0xc8844 in _kill ()
#1  0xc86e0 in kill ()
#2  0xbfc8a in abort ()
#3  0xfb64 in malloc_verify ()
#4  0xf9e0 in malloc_verify ()
#5  0xeee0 in malloc ()
#6  0x73abe in XtMalloc ()
#7  0x582ec in _XmStringGetCurrentCharset ()
#8  0x55a20 in XmStringCreate ()
#9  0x55ac0 in XmStringCreateSimple ()
#10 0xc480 in acceptPress (widg=0x19d810) at acceptPress.c:59
#11 0x74064 in _XtCallCallbacks ()
#12 0x3010000 in end ()
Cannot access memory at address 0xdfff7918.
(gdb)
------------------
(acceptPress.c is the callback file)

Here's the code:
------------------
#include <Xm/Xm.h>
#include <help_req.h>

extern Widget msgList;
extern Widget acceptButton;
extern Widget helpShell;
extern Widget mainHelpShell;
extern Widget reallyCloseForm;
extern Widget reallyCloseText;
extern Widget mainAcceptButton;
extern igotit();
extern rescan();
extern dbaccess();
extern int commit();
extern getmsg();
extern int many,id;
extern int table_size;

extern struct help_req current_msg;

acceptPress(widg)
Widget widg;
{
    XmStringContext strcon;
    XmStringDirection whither;
    XmStringCharSet charset;
    char *writing;
    Boolean iffy;
    XmString test[0];
    XmString message;

    if (widg==mainAcceptButton) 
	{
	test[0]=XmStringCreateSimple("Close");
	XtVaSetValues(acceptButton,XmNlabelString,test[0],NULL);
	XmStringFree(test[0]);
	}
    XtVaGetValues(acceptButton,XmNlabelString,&test[0],NULL);
    if (XmStringInitContext(&strcon,test[0])!=FALSE)
	{
	if (XmStringGetNextSegment(strcon,&writing,&charset,&whither,&iffy)!=FALSE)
	    {
	    XmStringFree(test[0]);
	    dbaccess('R');
	    if (strstr(writing,"Accept"))
		{
		if (commit()==0)
		    {
		    if (igotit(current_msg.id,"Accept")!=0)
			{
			rescan();
			}
		    if (XtIsRealized(helpShell)) getmsg();
		    }
		}
	    else if (strstr(writing,"Close"))
		{
		sprintf(writing,"You are about to close Help Req. %d.\0",current_msg.id);
		message=XmStringCreateSimple(writing);
		XtVaSetValues(reallyCloseText,XmNlabelString,message,NULL);
		XmStringFree(message);
		XtManageChild(reallyCloseForm);
		}
	    dbaccess('O');
	    }
	XmStringFreeContext(strcon);
	}
}
------------------

Any ideas? Anyone??

Ronan.
-- 
Ronan Waide, Motorola Ireland	       .B.	waider@glas.rtsg.mot.com
                                     .Y   E.
                                    C   ?   R	Live Nowhere.
                                     P     K 	Believe Nothing.
Newsgroups: comp.windows.x.motif