Interactive PowerBasic Forum

IT-Consultant: Charles Pegge => OxygenBasic => Topic started by: Zlatko Vid on June 20, 2023, 09:46:42 PM

Title: Memory Leak in TaskManager
Post by: Zlatko Vid on June 20, 2023, 09:46:42 PM
Hello

When i working on my interpreter i found mem leak
when i looking into Windows Task manager

when program start i have somewhere cca 1368 kb used
most programs in interpreter don't make leaks including drawing all
possible fractals and graphics ..then there is no increase in memory usage
BUT
when i use WM_KEYDOWN message processing then mem leak become visible.
like i do:
CASE WM_KEYDOWN
         hWParam = wParam
         'call block winMsg
         if pos_wmKeyDown > 0
            'print "KEYDOWN:" + str(pos_wmKeyDown)
            tc = pos_wmKeyDown : tokInterpreter()
            tc = 0
         end if
I suspect that is because i called same interpreter function multiple times
when keyboard key is pressed.
I don't tested is this also happened when i do ordinary clicking on GUI button
but i will

memory goes up to 6240 kB (if i remember) then program just exit
simply is closed like when we use ExitProcess 0

Is there a way to prevent this memory consumption?
thanks
Title: Re: Memory Leak in TaskManager
Post by: Charles Pegge on June 20, 2023, 10:21:51 PM
You can get into a situation where a procedure calls itself indirectly, and never returns. A quick test for recursion is to look at the stack pointer at one position in the program, and see whether it goes down continuously.

sys pp=esp 'read the stack pointer
...

Stacks are usually around 1 meg and quickly overflow when recursion is uncontrolled. Whereas string memory leaks run into hundreds or even thousands of meg before crashing the system.


Title: Re: Memory Leak in TaskManager
Post by: Zlatko Vid on June 21, 2023, 12:00:27 AM
OK
but where to put this check?
sys pp=esp 'read the stack pointer
Title: Re: Memory Leak in TaskManager
Post by: Pierre Bellisle on June 21, 2023, 02:04:01 AM
Not sure if this apply to your case Aurel, in any case...
To avoid Windows message auto recursion, I often use a flag like this...
CASE WM_KEYDOWN
         static boolean OnceOnlyFlag
         hWParam = wParam
         'call block winMsg
         if pos_wmKeyDown > 0
           if OnceOnlyFlag = false
             OnceOnlyFlag = true
             'print "KEYDOWN:" + str(pos_wmKeyDown)
             tc = pos_wmKeyDown : tokInterpreter()
             tc = 0
             OnceOnlyFlag = false
           end if
         end if
Title: Re: Memory Leak in TaskManager
Post by: Zlatko Vid on June 21, 2023, 07:14:00 AM
Hi Pierre

Yes it looks that is auto recursion
and i will try your method
Title: Re: Memory Leak in TaskManager
Post by: Zlatko Vid on June 21, 2023, 12:19:50 PM
Unfortunately this method block function call
so execute just once and stop.
Title: Re: Memory Leak in TaskManager
Post by: Pierre Bellisle on June 21, 2023, 09:46:27 PM
So, if I follow corectly, it never come back from tokInterpreter() ?
If you put a MBOX("Back from tokInterpreter!") after  tokInterpreter(), it will never pop up ?
Title: Re: Memory Leak in TaskManager
Post by: Pierre Bellisle on June 21, 2023, 10:02:40 PM
About  Charles great suggestion, (maybe he forgot about your question)
You may put it where you think the leak occur...

sys pp=esp 'read the stack pointer to check for abnormal growing size
SetWindowText(hDlg, "stack pointer " & hex(pp))
Title: Re: Memory Leak in TaskManager
Post by: Zlatko Vid on June 22, 2023, 07:18:27 AM

QuoteSo, if I follow corectly, it never come back from tokInterpreter() ?
If you put a MBOX("Back from tokInterpreter!") after  tokInterpreter(), it will never pop up ?

Hi Pierre

I must test it ..last time i tested it return
because when i press key key respond.
I really must made demo program in interpreter to test it .

As i said before ..leak occurs only when i use WM_KEYDOWN
which call tokInterpreter() function each time key was pressed
the problem is ..i guess is -> i called already running function
ok i will post some code
Title: Re: Memory Leak in TaskManager
Post by: Zlatko Vid on June 22, 2023, 05:30:16 PM
Hi to all

Yes ...main problem was in multiple function calls
when already interpreter function is called
and  i remove that part from WM_KEYDOWN message and now is:

CASE WM_KEYDOWN
          hWParam = wParam
         'call block winMsg
           if pos_wmKeyDown > 0
               'print "KEYDOWN:" + str(pos_wmKeyDown)
               tc = pos_wmKeyDown :   hWParam = 0
               tc = 0   
           end if

then i get that work without leaks
that part is fine now (i hope)
then i have problem with hWparam variable which read wparam
also i figured that i must reset it to null after is executed
because as it is global ,hold old value and must be reseted to null.