Author Topic: GDi Leak Detector  (Read 3230 times)

guga

  • Moderator
  • Member
  • *****
  • Posts: 826
  • Assembly is a state of art.
    • RosAsm
GDi Leak Detector
« 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
« Last Edit: September 09, 2014, 12:35:47 AM by guga »
Coding in Assembly requires a mix of:
80% of brain, passion, intuition, creativity
10% of programming skills
10% of alcoholic levels in your blood.

My Code Sites:
http://rosasm.freeforums.org
http://winasm.tripod.com

Gunther

  • Member
  • *****
  • Posts: 3517
  • Forgive your enemies, but never forget their names
Re: GDi Leak Detector
« Reply #1 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
Get your facts first, and then you can distort them.

guga

  • Moderator
  • Member
  • *****
  • Posts: 826
  • Assembly is a state of art.
    • RosAsm
Re: GDi Leak Detector
« Reply #2 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.
Coding in Assembly requires a mix of:
80% of brain, passion, intuition, creativity
10% of programming skills
10% of alcoholic levels in your blood.

My Code Sites:
http://rosasm.freeforums.org
http://winasm.tripod.com

Siekmanski

  • Member
  • *****
  • Posts: 1145
Re: GDi Leak Detector
« Reply #3 on: September 08, 2014, 11:47:52 PM »
Very handy, thanks.  :t

Gunther

  • Member
  • *****
  • Posts: 3517
  • Forgive your enemies, but never forget their names
Re: GDi Leak Detector
« Reply #4 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
Get your facts first, and then you can distort them.

guga

  • Moderator
  • Member
  • *****
  • Posts: 826
  • Assembly is a state of art.
    • RosAsm
Re: GDi Leak Detector
« Reply #5 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
Coding in Assembly requires a mix of:
80% of brain, passion, intuition, creativity
10% of programming skills
10% of alcoholic levels in your blood.

My Code Sites:
http://rosasm.freeforums.org
http://winasm.tripod.com

jj2007

  • Member
  • *****
  • Posts: 7752
  • Assembler is fun ;-)
    • MasmBasic
Re: GDi Leak Detector
« Reply #6 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 ::)

guga

  • Moderator
  • Member
  • *****
  • Posts: 826
  • Assembly is a state of art.
    • RosAsm
Re: GDi Leak Detector
« Reply #7 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:
Coding in Assembly requires a mix of:
80% of brain, passion, intuition, creativity
10% of programming skills
10% of alcoholic levels in your blood.

My Code Sites:
http://rosasm.freeforums.org
http://winasm.tripod.com

Gunther

  • Member
  • *****
  • Posts: 3517
  • Forgive your enemies, but never forget their names
Re: GDi Leak Detector
« Reply #8 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 ::)

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

Gunther
Get your facts first, and then you can distort them.