WB3D Memory Leak?

Blitz3D Forums/Blitz3D Userlibs/WB3D Memory Leak?

Danny(Posted 2008) [#1]
Hi Kev,

Is it correct the drawing function in wb3d have a memory leak?
I'm talking about WB3D_CreateWB3DRect/Line/Circle/Plot(), or am misunderstanding how to use them? I understand you typically use them on a PAINT_EVENT; and they don't return any 'gadget handles' you typically would have to purge after use.

Run the following example and check blitzcc.exe's memory consumption in the TaskManager, which simply keeps increasing..
Also after a minute or so it slows down when weird graphic flashes start appearing and the runtime window becomes unresponsive..



It would be a tremendous shame if we couldn't use these functions :((

Thanks,
Danny.


Danny(Posted 2008) [#2]
Here's an old hack i'm still using btw, just for rectangles. It doesn't seem to leak memory - but I have no idea if this is safe to use. I'm no api expert live Kev of course..

Note; requieres WB3D library, user32.decls and gdi32.decls


;from winGDI.h
Const DC_BRUSH		= 18
Const DC_PEN		= 19

Function WB3DXTRA_DrawRect( gadget, x1,y1, x2,y2, faceColor, borderColor )
	
; Note: Rectangle is NON PERMANENT! It needs to be re-painted after any event to ensure it's visibility.
	
	;get device contect
	hdc = api_GetDC( gadget )
	
	;validate
	If Not hdc Then Return
	
	;limit dimensions
	w = WB3D_GadgetWidth(gadget)
	h = WB3D_GadgetHeight(gadget)
	If x1 < 0 Then x1 = 0
	If y1 < 0 Then y1 = 0
	If x2 < 0 Then x2 = 0
	If y2 < 0 Then y2 = 0
	If x1 > w Then x1 = w
	If y1 > h Then y1 = h
	If x2 > w Then x2 = w
	If y2 > h Then y2 = h
	
	;convert RGB to BGR
	BrushCol = (((faceColor And $ff0000) Shr 16) + (((faceColor And $00ff00) Shr 8)*$100) + ((faceColor And $0000ff)*$10000))
	PenCol = (((borderColor And $ff0000) Shr 16) + (((borderColor And $00ff00) Shr 8)*$100) + ((borderColor And $0000ff)*$10000))
	
	;get brush object
	api_SelectObject hdc, api_GetStockObject(DC_BRUSH)
	api_SetDCBrushColor hdc, brushCol
	
	;get pen object
	api_SelectObject hdc, api_GetStockObject(DC_PEN)
	api_SetDCPenColor hdc, penCol
	
	;draw rectangle
	api_Rectangle hdc, x1,y1, x2,y2
	
	;purge temp resources
	api_ReleaseDC gadget, hdc
	
End Function


D.


Kev(Posted 2008) [#3]
ok confirmed theres a memory leak with direct dc drawing, i cant say if/when i will be fixed sorry. there should be no problem using the posted code danny looks good to me.

kev


John Blackledge(Posted 2008) [#4]
hdc's have always had to be released in Windows programming and this has been the bane of many a C programmer's life, and the cause of 99% of Windows memory leaks.

So the problem is not with WB3D as such; it's been there for 20 years.


Kev(Posted 2008) [#5]
Heres the .dll with the mem leak fix for all direct dc drawing.

http://www.whitegatesoftware.com/MemLeakFIX.zip

kev


Danny(Posted 2008) [#6]
10 Print "No way... May you rock forever Kev!"
20 Goto 10



John Blackledge(Posted 2008) [#7]
Hang on, doesn't api_GetStockObject() create a memory object that must be freed before api_ReleaseDC ? (-it's been a long time.)


Danny(Posted 2008) [#8]
Hi John,

Good chance you're right; however, I just put the above function in a loop and let it run for a few minutes and couldn't "detect" any memory increase in taskmanager.. So I'm guessing/hoping it's 'safe enough' at least ;)

D.


Nate the Great(Posted 2008) [#9]
just a question what is wb3d for? I haven't heard of it.


Abrexxes(Posted 2008) [#10]


:)