Page 1 of 1

6.01 Weird container / canvas memory leak

Posted: Fri Mar 17, 2023 10:22 pm
by Justin
If you run this code, grab a window corner and start resizing the window constantly the ram usage of the program keeps increasing until you stop resizing the window. W10 X64

Code: Select all

EnableExplicit

Procedure on_size()
	ResizeGadget(1, 0, 0, WindowWidth(0) / 2, WindowHeight(0) / 2)
	ResizeGadget(2, WindowWidth(0) / 2, 0, WindowWidth(0) / 2, WindowHeight(0) / 2)
	ResizeGadget(3, 0, WindowHeight(0) / 2, WindowWidth(0) / 2, WindowHeight(0) / 2)
	ResizeGadget(4, WindowWidth(0) / 2, WindowHeight(0) / 2, WindowWidth(0) / 2, WindowHeight(0) / 2)

; 	ResizeGadget(5, 0, 0, WindowWidth(0), WindowHeight(0))
EndProcedure

OpenWindow(0, 10, 10, 600, 400, "Test", #PB_Window_SizeGadget | #PB_Window_MaximizeGadget | #PB_Window_MinimizeGadget | #PB_Window_SystemMenu)
BindEvent(#PB_Event_SizeWindow, @on_size())
ContainerGadget(1, 0, 0, 0, 0)
CloseGadgetList()
ContainerGadget(2, 0, 0, 0, 0)
CloseGadgetList()
ContainerGadget(3, 0, 0, 0, 0)
CloseGadgetList()
ContainerGadget(4, 0, 0, 0, 0)
CloseGadgetList()

SetGadgetColor(1, #PB_Gadget_BackColor, RGB(255, 0, 0))
SetGadgetColor(2, #PB_Gadget_BackColor, RGB(0, 255, 0))
SetGadgetColor(3, #PB_Gadget_BackColor, RGB(0, 0, 255))
SetGadgetColor(4, #PB_Gadget_BackColor, RGB(255, 0, 255))

; ButtonGadget(5, 0, 0, 0, 0, "Test")
on_size()

Repeat

Until WaitWindowEvent() = #PB_Event_CloseWindow

Re: 6.01 Weird container / canvas memory leak

Posted: Fri Mar 17, 2023 11:54 pm
by infratec
With PB 6.01 b5 Windows x86 on Win10 x64 (Assembler backend)
I have 1.9MB after start.
During continous resizing it grows up to 2.7MB.
But after I stop resizing it went back to 1.,9MB.

What are your values? C or Asm?
And does the memory usage stays high after resizing?

I will update to 6,01 final now.

Re: 6.01 Weird container / canvas memory leak

Posted: Sat Mar 18, 2023 12:10 am
by infratec
Now with 6.01 final:

In all combinations, the x86 version went always back with the memory usage.
It is not constantly growing.

Re: 6.01 Weird container / canvas memory leak

Posted: Sat Mar 18, 2023 12:13 am
by skywalk
Hard to confirm. I disable animations on Windows 10 PRO.

After compiling x64 with CBE+optimizer.
1308k ram on start.
1312k ram after resize.
1312k ram after repeated resizing.

Re: 6.01 Weird container / canvas memory leak

Posted: Sat Mar 18, 2023 6:08 am
by Denis
Not confirmed on my computer.
Window 11 X64 Pro (version 22H2)
32 GB RAM
PB 6.01 LTS X64

All compilation with debugger Off.

compiling x64 with ASM witout optimizer.
1312k ram on start.
1312k ram after first resizing.
1288k ram after repeated resizing.

compiling x64 with ASM with optimizer.
1300k ram on start.
1308k ram after first resizing.
1308k ram after repeated resizing.

compiling x64 with CBE witout optimizer.
1276k ram on start.
1276k ram after resize.
1312k ram after repeated resizing.

compiling x64 with CBE with optimizer.
1276k ram on start.
1276k ram after resize.
1312k ram after repeated resizing.

Re: 6.01 Weird container / canvas memory leak

Posted: Sat Mar 18, 2023 12:12 pm
by Justin
Tested on three different computers W10 Pro X64, same results on all of them, 6.01 Final, default compiler C Backend no longer appears in final. Debugger enabled / disabled makes no difference.
In all cases when you stop resizing the ram goes to normal.

How long do you keep resizing the window? You have to do it constantly without releasing the mouse. After one minute i reach 8MB easily and grows until i release the mouse.
Image

Re: 6.01 Weird container / canvas memory leak

Posted: Sat Mar 18, 2023 1:44 pm
by mk-soft
It is a Windows internal story with the memory management.

With windows I sometimes minimize the window before I hide it (HideWindow) so I force windows to clean up the memory.

Re: 6.01 Weird container / canvas memory leak

Posted: Sat Mar 18, 2023 4:44 pm
by Justin
I am aware of that windows memory story. But this looks more like a PB issue, if you run the following code that uses a pure api container and does not use BindEvent() the memory stays at 1.4MB rock solid, it never goes past that no matter how heavily you resize the window for any amount of time.

I would say is some event data or drawing object not being released, maybe is inherent to windows memory management or to PB. Not a big problem because the memory is released when the resizing ends, but it is something Fred could look at.

Code: Select all

EnableExplicit

#CONT_CLASS = "mycont"

Global.i hwCont

Declare on_size()

Procedure contProc(hwnd.i, msg.l, wparam.i, lparam.i)
	Select msg
		Case #WM_PAINT
			Protected.PAINTSTRUCT ps
			Protected.i hdc, hbr
			
			hdc = BeginPaint_(hwnd, @ps)
			hbr = CreateSolidBrush_(RGB(255, 0, 0))
			FillRect_(hdc, @ps\rcPaint, hbr)
			DeleteObject_(hbr)
			EndPaint_(hwnd, @ps)
			ProcedureReturn 0
		
	EndSelect
	
	ProcedureReturn DefWindowProc_(hwnd, msg, wparam, lparam)
EndProcedure

Procedure wnd_proc(hwnd.i, msg.l, wparam.i, lparam.i)
	Select msg
		Case #WM_SIZE
; 			on_size()
			MoveWindow_(hwCont, 0, 0, DesktopScaledX(WindowWidth(0)), DesktopScaledY(WindowHeight(0)), #True)
			ProcedureReturn 0
			
	EndSelect
	
	ProcedureReturn #PB_ProcessPureBasicEvents
EndProcedure

Procedure registerClass()
	Protected.WNDCLASS wc
	Protected.s className
	
	className = #CONT_CLASS
	wc\hInstance = GetModuleHandle_(0)
	wc\lpfnWndProc = @contProc()
	wc\style = #CS_HREDRAW | #CS_VREDRAW
	wc\lpszClassName = @className
	
	RegisterClass_(@wc)
EndProcedure

Procedure on_size()
; 	ResizeGadget(1, 0, 0, WindowWidth(0) / 2, WindowHeight(0) / 2)
; 	ResizeGadget(2, WindowWidth(0) / 2, 0, WindowWidth(0) / 2, WindowHeight(0) / 2)
; 	ResizeGadget(3, 0, WindowHeight(0) / 2, WindowWidth(0) / 2, WindowHeight(0) / 2)
; 	ResizeGadget(4, WindowWidth(0) / 2, WindowHeight(0) / 2, WindowWidth(0) / 2, WindowHeight(0) / 2)

; 	ResizeGadget(5, 0, 0, WindowWidth(0), WindowHeight(0))

	MoveWindow_(hwCont, 0, 0, DesktopScaledX(WindowWidth(0)), DesktopScaledY(WindowHeight(0)), #True)
EndProcedure

registerClass()
OpenWindow(0, 10, 10, 600, 400, "Test", #PB_Window_SizeGadget | #PB_Window_MaximizeGadget | #PB_Window_MinimizeGadget | #PB_Window_SystemMenu)
; BindEvent(#PB_Event_SizeWindow, @on_size())

hwCont = CreateWindowEx_(0, #CONT_CLASS, "", #WS_CHILD | #WS_VISIBLE, 0, 0, 0, 0, WindowID(0), 0, GetModuleHandle_(0), 0)

; ContainerGadget(1, 0, 0, 0, 0)
; CloseGadgetList()
; ContainerGadget(2, 0, 0, 0, 0)
; CloseGadgetList()
; ContainerGadget(3, 0, 0, 0, 0)
; CloseGadgetList()
; ContainerGadget(4, 0, 0, 0, 0)
; CloseGadgetList()
; 
; SetGadgetColor(1, #PB_Gadget_BackColor, RGB(255, 0, 0))
; SetGadgetColor(2, #PB_Gadget_BackColor, RGB(0, 255, 0))
; SetGadgetColor(3, #PB_Gadget_BackColor, RGB(0, 0, 255))
; SetGadgetColor(4, #PB_Gadget_BackColor, RGB(255, 0, 255))

SetWindowCallback(@wnd_proc())

; ButtonGadget(5, 0, 0, 0, 0, "Test")
on_size()

Repeat

Until WaitWindowEvent() = #PB_Event_CloseWindow


Re: 6.01 Weird container / canvas memory leak

Posted: Sat Mar 22, 2025 10:21 am
by Fred
What you are experiencing is the internal resize event stacking in PB because the event loop is locked when resizing the window (so we have a separate queue to process them when WindowEvent() is called). Once the mouse is release, all the stacked events are handled and freed, so there is no bug or leak here.