Path: utzoo!utgpu!news-server.csri.toronto.edu!rpi!usc!apple!REEKES@applelink.apple.com From: REEKES@applelink.apple.com (Jim Reekes) Newsgroups: comp.sys.mac.programmer Subject: Re: What am I doing wrong?--modal dialog use crashes Message-ID: <13405@goofy.Apple.COM> Date: 8 May 91 18:05:35 GMT References: <1991May7.234354.26431@agate.berkeley.edu> Sender: usenet@Apple.COM Organization: Apple Computer, Inc. Lines: 63 In article <1991May7.234354.26431@agate.berkeley.edu>, labc-1ic@e260-1c.berkeley.edu (Willy S. Liao) writes: > > OK dokee, I have a problem that's driving me nuts. I'm sure I'm doing > something wrong, but I don't know what it is...I'm using THINK C 4.04 with > System 7.0f4 and the Think Class Library. The following function is invoked > by my document initialization method (it gets an integer for the size of the > game board). Whenever this function gets run with NO other windows on the > screen, it works fine. However, if there is another window open in the > application layer (i.e. another instance of a document is in memory), this > function chokes with a syserr 28 (stack has moved into heap) error on the > call to ModalDialog(). Can anyone suggest anything? Boosting the memory > partition up to larger sizes doesn't help (although the crash sometimes > turns into an error 25, out of memory). > > My function: > > static short > GetBoardsize(void) > { > Str255 req, answer; > DialogPtr dptr; > short temp; > long size; > Rect rect; > Handle ihandle; > DialogRecord dRec; > > GetIndString(req, GAME_MSGS, GET_SIZE_STR); > ParamText(req, "\p","\p","\p"); > dptr = GetNewDialog(GENERAL_REQ_DIALOG, &dRec, NULL); > GetDItem(dptr, GENERAL_ETEXT_NUM, &temp, &ihandle, &rect); > do { > NumToString((long) DEFAULT_BSIZE, answer); > SetIText(ihandle, answer); > SelIText(dptr, GENERAL_ETEXT_NUM, 0, (short) answer[0]); > ShowWindow(dptr); > ModalDialog(NULL, &temp); > GetDItem(dptr, GENERAL_ETEXT_NUM, &temp, &ihandle, &rect); > GetIText(ihandle, answer); > StringToNum(answer, &size); > } while (size < MIN_BOARDSIZE || size > MAX_BOARDSIZE); > CloseDialog(dptr); > DisposHandle(dRec.items); > return (short) size; > } This line is bad: dptr = GetNewDialog(GENERAL_REQ_DIALOG, &dRec, NULL); You never allocated any memory for dRec. You have an uninitialized pointer, and passed it to the Dialog Manager for storage. This is going to cause random results. Either allocate some storage with NewPtr(sizeof(DialogRecord)) or pass nil. Refer to Inside Mac "The Dialog Manager" chapter pages 413-414. Also, you don't need to call ShowWindow every time through the loop. Are you resetting GENERAL_ETEXT_NUM to DEFAULT_BSIZE everytime through the loop? Finally, you should do some reality checking on the text of GENERAL_ETEXT_NUM before calling StringToNum. If some non-numeric data was entered, random results will be returned by StringToNum Jim Reekes E.O., Macintosh Toolbox Engineering