The unit code with the noload option will remain in memory until the calling procedure is exited. This may result in a stack overflow (out of memory) if control never exits from such a unit after it is called repeatedly from inside the same procedure. This is a known problem, and can be avoided by specifying the unit as "Resident" within that procedure or segment. Calling the unit from within a different procedure will also remedy this. The following examples illustrate this situation:
EXAMPLEA below will generate an out of memory STACK OVERFLOW error by repeatedly loading APPLESTUFF.
PROGRAM EXAMPLEA;
USES APPLESTUFF;
VAR I: INTEGER;
BEGIN (* MAIN PROGRAM *)
(*$N+*)
WRITE ('NUMBER OF WORDS AVAILABLE AT START OF PROGRAM : ');
WRITELN (MEMAVAIL);
FOR I:=1 TO 100 DO BEGIN
WRITELN (I,' ',MEMAVAIL,' WORDS');
NOTE (25,1) (* APPLESTUFF MUSIC ROUTINE *)
END;
WRITELN ('PROGRAM COMPLETED SUCCESSFULLY')
END. (* MAIN PROGRAM *)
EXAMPLEB uses the RESIDENT OPTION to prevent APPLESTUFF from being reloaded.
PROGRAM EXAMPLEB;
USES APPLESTUFF;
VAR I: INTEGER;
BEGIN (* MAIN PROGRAM *)
(*$N+*)
(*$R APPLESTUFF *)
WRITE ('NUMBER OF WORDS AVAILABLE AT START OF PROGRAM : ');
WRITELN (MEMAVAIL);
FOR I:=1 TO 100 DO BEGIN
WRITELN (I,' ',MEMAVAIL,' WORDS');
NOTE (25,1) (* APPLESTUFF MUSIC ROUTINE *)
END;
WRITELN ('PROGRAM COMPLETED SUCCESSFULLY')
END. (* MAIN PROGRAM *)
EXAMPLEC uses individual procedure to call APPLESTUFF routine, which allows unit to be released and then reloaded.
PROGRAM EXAMPLEC;
USES APPLESTUFF;
VAR I: INTEGER;
PROCEDURE PLAY;
BEGIN WRITELN (I,' ',MEMAVAIL,' WORDS');
NOTE (25,1) (* APPLESTUFF MUSIC ROUTINE *)
END;
BEGIN (* MAIN PROGRAM *)
(*$N+*)
WRITE ('NUMBER OF WORDS AVAILABLE AT START OF PROGRAM : ');
WRITELN (MEMAVAIL);
FOR I:=1 TO 100 DO PLAY;
WRITELN ('PROGRAM COMPLETED SUCCESSFULLY')
END. (* MAIN PROGRAM *)