The MASM Forum

Projects => Rarely Used Projects => RosAsm => Topic started by: guga on September 08, 2014, 09:59:35 PM

Title: GDi Leak Detector
Post by: guga on September 08, 2014, 09:59:35 PM
A set of small functions to detect GDI leaks

Code: [Select]
; pGuiCount: Pointer to a variable that will hold the total amount of objects used on the current process

Proc StartGdiCapture:
    Arguments @pGuiCount
    Uses ecx, edx, edi, eax

    call 'KERNEL32.GetCurrentProcess'
    call 'user32.GetGuiResources' eax, &GR_GDIOBJECTS
    mov edi D@pGuiCount
    mov D$edi eax

EndP

Code: [Select]
;;
    GuiCount: The total amount of objects previously retrieved by StartGdiCapture

    Returned Value
         If the function returns 0, it means there is no leaks
               if eax is bigger then Zero, it means you have a gdi leak
               A negative value  means the gdi objects are being released (When you call DestroyWindow, for example)
;;


Proc IsGdiLeaking:
    Arguments @GuiCount
    Uses ecx, edx, edi

    call 'KERNEL32.GetCurrentProcess'
    call 'user32.GetGuiResources' eax, &GR_GDIOBJECTS
    sub eax D@GuiCount

EndP


Example of usage

Code: [Select]
[GdiCount: D$ 0]
call StartGdiCapture GdiCount

call MyRoutine

call IsGdiLeaking D$GdiCount

If eax >s 0 ; If eax is positive and bigger then 0
     ; Found leak. do something
End_If


References and issues for this functions:
http://stackoverflow.com/questions/18454380/how-to-correct-the-gdi-resource-leakage
http://blogs.msdn.com/b/oldnewthing/archive/2010/09/23/10066473.aspx
http://msdn.microsoft.com/en-us/magazine/cc301756.aspx
http://msdn.microsoft.com/en-us/library/windows/desktop/dd144844%28v=vs.85%29.aspx
http://msdn.microsoft.com/en-us/library/windows/desktop/ms683192%28v=vs.85%29.aspx
https://www.relisoft.com/win32/gdileaks.html
Title: Re: GDi Leak Detector
Post by: Gunther on September 08, 2014, 10:15:25 PM
Hi Gustavo,

thank you for the code and the interesting links. Very good background information.  :t

Gunther
Title: Re: GDi Leak Detector
Post by: guga on September 08, 2014, 10:52:43 PM
Hi Guther

You´re welcome  :t

I saw that info an hour ago and made those small functions to give a test. It seems to work as expected.
Title: Re: GDi Leak Detector
Post by: Siekmanski on September 08, 2014, 11:47:52 PM
Very handy, thanks.  :t
Title: Re: GDi Leak Detector
Post by: Gunther on September 09, 2014, 01:09:35 AM
Hi Gustavo,

I saw that info an hour ago and made those small functions to give a test. It seems to work as expected.

yes it does. Moreover, these are important informations for graphics programming.

Gunther
Title: Re: GDi Leak Detector
Post by: guga on September 09, 2014, 02:46:36 AM
Tks Siekmanski and Gunther

I hope it helps others. I was reading in one of those articles about using gdiflush whenever you change from memory DC to the loaded file in memory. I didn´t tested the gdiflush to see if it really releases the cache as it is reported here (http://stackoverflow.com/questions/18454380/how-to-correct-the-gdi-resource-leakage)
Title: Re: GDi Leak Detector
Post by: jj2007 on September 09, 2014, 03:26:20 AM
Hi Gustavo,

Haven't seen GDI leaks for a while but there were times when I would have desperately needed this tool :icon14:

P.S.: "10% of alcoholic levels in your blood." - are you sure? That is far off the Ballmer peak (http://xkcd.com/323/) ::)
Title: Re: GDi Leak Detector
Post by: guga on September 09, 2014, 03:30:24 PM
Hi JJ

Tks :biggrin:

Quote
P.S.: "10% of alcoholic levels in your blood." - are you sure? That is far off the Ballmer peak

Ok, ok 10% is too much  :icon_mrgreen: :icon_mrgreen: But i´m used to drink one beer or two while coding  :icon_mrgreen:
Title: Re: GDi Leak Detector
Post by: Gunther on September 10, 2014, 02:09:37 AM
Jochen,

P.S.: "10% of alcoholic levels in your blood." - are you sure? That is far off the Ballmer peak (http://xkcd.com/323/) ::)

that was a good one.  :lol: Do you have some background information about that?

Gunther