[comp.sys.mac.programmer] Question on simple backgrounding

fons@uxh.cso.uiuc.edu (02/06/89)

I am very new to programming the mac and wanted to do something quick and
dirty to allow a number crunching application to run in the background (and
write to files -- using LSC 3.0).  I just tried calling WaitNextEvent looking
for command period keycodes.  It seems to work, the command period is detected
(which interupts the calculation) and foreground tasks seem to run at near 
normal speed.  The trouble is that looking at the memory gauge under MF
(by pulling it down from about the finder under the Apple menu) the job
asks for more and more memory.  It increases gradually in ~2K chunks and
sucks up all the memory.  Does anyone have any idea what I am doing wrong?

Here is an example of what I wrote:

/* Try doing some calculations using event manager WaitNextEvent */
#include <unix.h>
void exit(void);
main()
{
	RgnHandle mouseRgn;
	long count=0;
	int backgroundTask();
	
	printf("\n Starting count now ");
	mouseRgn = (RgnHandle) NewHandle(sizeof(Region));
	printf("\n The size of a region is %d \n",sizeof(Region));
	(*mouseRgn)->rgnSize = 10;
	(*mouseRgn)->rgnBBox.top = 0;
	(*mouseRgn)->rgnBBox.bottom = 0;
	(*mouseRgn)->rgnBBox.right = 0;
	(*mouseRgn)->rgnBBox.left = 0;
	
	for(count=0;count<10000;count++)
		{
			if (backgroundTask(mouseRgn))
				{
					printf("\n  Quitting \n ");
					exit();
				}
			printf("\rloop tick: %ld",count);
		}
}

int backgroundTask(mouseRgn)
	RgnHandle mouseRgn;
{
	static EventRecord myevent;
	static waittime=30;
	static int whatChar,commandKey;
			WaitNextEvent(keyDownMask,&myevent,waittime,mouseRgn);
			whatChar = (myevent.message & keyCodeMask);
			commandKey = (myevent.modifiers & cmdKey);
			if (commandKey && (whatChar==0x2b00))
				{
					printf("\n Flushing Events \n");
					FlushEvents(everyEvent,0);
				}
			if (commandKey && (whatChar==0x2f00))
				{	
					printf("\n Quit command Received\n");
					return(1);

				}
			return(0);
}


Does anyone have any ideas as to what might be causing this memory drain?
Perhaps if there were an example program to work from I could just use that
especially if there was such a thing as a scrollable TE window.
                                                        Paul Fons
                                        University of Illinois
                                        Coordinated Science Laboratory
                                        1101 W. Springfield Av.
                                        Urbana, Illinois 61801 U.S.A.
                              email:   Fons@uiucvmd.bitnet or...
                                        Fons@uxh.cso.uiuc.edu