[comp.sys.mac.programmer] A strange MPW bug

phil@mit-amt.MEDIA.MIT.EDU (Phil Sohn) (06/01/88)

I have a really wierd bug, does anyone have an idea what is going on?
In the following code segment there are two bugs.  The first one is I
can not seem to get a string back out of a structure once I put it there.
The second one is DebugStr does not seem to work like I think it should.
The first and second calls to DebugStr prints the correct error message
in MacsBug, but the third prints almost garbage (instead of printing the
string that is determined at compile time, it prints part of the title
that was passed in as an argument, and then a procedure name that is
defined elsewhere.)   I assume that the string pool is getting screwed
someplace, but I can not find where.  The files are compiled with the -g
option, and I assume that is where the procedure name is comming from.

I assume the contrlTitle of the control returned by NewControl is a Pascal
String.  I can not seem to get this sting back after the call.

short Make_ScrollBar(ctrl,win,x,y,w,h,title,val,min,max)
	ControlHandle *ctrl;
	WindowPtr win;
	short x,y,w,h,val,min,max;
	char *title;
{
	ControlHandle c;
	Rect r;

	DebugStr("The struct title is ");  /* This print correctly */
	r.top=y; r.left=x;
	r.bottom=y+h; r.right=x+w;
	DebugStr(strcat("On entry title   is ", title)); /* This to */
	DebugStr("The struct title is "); /* This prints garbage i.e.
             the value of title followed by ssssDrawScrollBar() */
	c = NewControl(win,&r,title,true,val,min,
		       max,scrollBarProc,(long)scrollBarProc);
	DebugStr(p2cstr((*c)->contrlTitle)); /* this is where the first bug
 						is shown.  Shouldn't
						p2cstr((*c)->contrlTitle
						return title which was
						passed to NewControl?  */
	if(c) {
		*ctrl = c;
		return(true);
	} else
		return(false);
}



Thanks for any help you can provide.


					Arpa: phil@ems.media.mit.edu

dhare%integral@Sun.COM (Dwight Hare) (06/01/88)

In article <2554@mit-amt.MEDIA.MIT.EDU> phil@mit-amt.MEDIA.MIT.EDU (Phil Sohn) writes:
>I have a really weird bug, does anyone have an idea what is going on?
>In the following code segment there are two bugs.  The first one is I
>can not seem to get a string back out of a structure once I put it there.
>The second one is DebugStr does not seem to work like I think it should.
>	DebugStr(strcat("On entry title   is ", title)); /* This to */
>	DebugStr("The struct title is "); /* This prints garbage i.e.
>             the value of title followed by ssssDrawScrollBar() */

This is an ordinary C programming mistake.  Strcat appends to the string
by writing over the terminating zero and continuing on.  There is no
concept of extending the string.  Since the string provided has no
space alloted for appending, the title writes over whatever follows the
string in memory, which appears to be the string used in the next DebugStr
call.  Once you overwrite memory, everything gets confused.

To do what you want would require:

char str[100];

strcpy(str, "On entry title is ");
strcat(str, title);
DebugStr(str);