Memory leak in program execution?

Post here to share useful tips and tricks, to ask questions about using your DM42 or to report software-related problems
Post Reply
Florian
Posts: 23
Joined: Mon Apr 26, 2021 1:19 pm
Location: Japan

Memory leak in program execution?

Post by Florian »

I am continuing to experiment with the HP82240A printer and trying my hands at yet another plotting program. For this my plan was to start with a rough framework to print a frame around the plot, tic marks and tic labels.

During my testing I suddenly ran into an "Insufficient memory" message which puzzled me quite a bit. After some poking around I saw that with every run of the program I loose exactly 1600 bytes of free memory until at some point I am left with nothing. To recover the lost memory it is sufficient to save the calculator state. Running the program again leads to the same sequential memory loss.

To me the program seems nothing special, using only a bunch of loops and local variables. I tried to isolate different parts of the code into a separate program but was not successful in reproducing the memory leak. Therefore I will have to post the full code here...

My first try was to run the RAW file through the DM42 programming tool. This tool, however, fails with "Invalid TEXT subcommand at PC:00BE". The command at 00BE seems to be the newly introduced "X≥?" (line 40), so this is probably not yet supported by the programming tool. Instead, I printed the program to a txt file then. The original RAW file is, just in case, also attached.

Code: Select all

00 { 342-Byte Prgm }
01▶LBL "FPLOT"
02 RCL "GrMod"	@@@ save original graphics mode
03 LSTO "_GrMod"
04 0
05 STO "GrMod"
06 0			@@@ set up plotting range and steps
07 LSTO "_XMIN"
08 5
09 LSTO "_XMAX"
10 2
11 LSTO "_XSTEP"
12 0
13 LSTO "_YMIN"
14 1
15 LSTO "_YMAX"
16 RCL "_XSTEP"
17 RCL÷ "ResY"
18 LSTO "_DX"
19 RCL "_YMAX"
20 RCL- "_YMIN"
21 RCL÷ "ResX"
22 LSTO "_DY"

23▶LBL F		@@@ main printing function
24 RCL "_XMIN"
25 LSTO "_XPOS"
26 CLLCD
27 1
28 XEQ 51		@@ initial horizontal line

29▶LBL 11		@@@ outer loop: divide plot range into single screens
30 RCL "ResY"
31 1000
32 ÷
33 1
34 +
35 LSTO "_YSCR"
36▶LBL 12		@@@ inner loop: single screen contents
37 RCL "_DX"
38 STO+ "_XPOS"
39 RCL "_XPOS"
40 X≥? "_XMAX"
41 GTO 19		@@ at end of plotting range
42 RCL "_YSCR"
43 XEQ 52		@@ left and right border
44 ISG "_YSCR"
45 GTO 12		@@ next pixel line of current screen
46 PRLCD
47 CLLCD		@@ clear screen in preparation of next segment
48 XEQ 53		@@ tic marks of new screen segment
49 GTO 11		@@ go to next screen segment
50 GTO 99		@@ (my mistake: this line is not needed anymore)

51▶LBL 19		@@@ print final horizontal line
52 RCL "_YSCR"
53 XEQ 51
54 PRLCD		@@ print this remaining part
55 ADV
56 GTO 99		@@ finished

57▶LBL 51		@@@ print horizontal line at ST X
58 +/-
59 0
60 PIXEL
61 R↓
62 +/-
63 RTN

64▶LBL 52		@@@ print left and right border
65 1
66 PIXEL
67 R↓
68 RCL "ResX"
69 PIXEL
70 R↓
71 RTN

72▶LBL 53		@@@ print left and right tic marks in row 1
73 "××××"
74 1
75 2
76 AGRAPH
77 1
78 RCL "ResX"
79 4
80 -
81 AGRAPH
82 "←"			@@ start to prepare tic label
83 ARCL "_XPOS"
84 3
85 1
86 XEQ "DISPLAY"	@@ Bill's DISPLAY function for in-graphics text
87 RTN

88▶LBL 99		@@@ restore original graphics mode and exit
89 RCL "_GrMod"
90 STO "GrMod"
91 END
What I could confirm so far is that neither the call to Bill's DISPLAY (line 86) nor the various PRLCD are causing this reduction of memory.

My DM42 info: DMCP v3.21, DM42 v3.18

Any ideas? Any additional information I can provide?

Cheers
Florian
Attachments
FPLOT.raw
(345 Bytes) Downloaded 118 times
dlachieze
Posts: 613
Joined: Thu May 04, 2017 12:20 pm
Location: France

Re: Memory leak in program execution?

Post by dlachieze »

Just to clarify: in which stack mode are you running your program ? Classic 4-level stack or the new N-level stack?
DM42: 00425 - DM41X: β00066 - WP43: 00042
Florian
Posts: 23
Joined: Mon Apr 26, 2021 1:19 pm
Location: Japan

Re: Memory leak in program execution?

Post by Florian »

Ah right, I forgot about that one: 4-level stack. "Dynamic Stack Extension" is disabled in the Settings menu.
Florian
Posts: 23
Joined: Mon Apr 26, 2021 1:19 pm
Location: Japan

Re: Memory leak in program execution?

Post by Florian »

I think I got the memory leak pretty much narrowed down now. After some more trial and error I came up with a rather minimal example.

Code: Select all

LBL "TEST"
1
STO "A"
0
LBL 01
X>? "A"
RTN
1
+
GTO 01
END
With each execution of above code I lose 120 bytes of memory.

Replacing the "X>?" command by a more traditional "X>Y" construction does not lead to this loss. So my uneducated guess would be that the "X>?" instruction is leaking some memory.
Thomas Okken
Posts: 1102
Joined: Tue May 02, 2017 5:48 pm
Location: Netherlands
Contact:

Re: Memory leak in program execution?

Post by Thomas Okken »

Florian wrote:
Thu Apr 29, 2021 1:58 am
Replacing the "X>?" command by a more traditional "X>Y" construction does not lead to this loss. So my uneducated guess would be that the "X>?" instruction is leaking some memory.
Confirmed. The same applies to all the new comparison functions. I'll fix this in 3.0.3.
Florian
Posts: 23
Joined: Mon Apr 26, 2021 1:19 pm
Location: Japan

Re: Memory leak in program execution?

Post by Florian »

Great. Thanks for the quick confirmation.

Now I'll just have to find a workaround on my DM42 so that I can continue to write my plotting program without running out of memory till the fix is picked up in the DM42 firmware...
Post Reply