Fixing Window Region Memory Leak

Fixing Window Region Memory Leak

Post by wxforecast » Sat, 23 Dec 2006 05:37:09

The code below is used to create a transparent window region around some
controls on a form -- it works just fine.

Everytime I execute the code below however, I'm eating some serious GDI
resources due to an apparent memory leak. My guess is that it's because I'm
creating a number of separate instances of TmpRegn through repeated use of
CreateRectRgn in the for loop, and my cleanup code only removes the most
recent one???

What's a better way to handle this? Make an array of TmpRegn??


Dim Regn As Long
Dim TmpRegn As Long
Dim TmpControl As Control

'makes everything invisible
Regn = CreateRectRgn(0, 0, 0, 0)

'A loop to check every control in the form
For Each TmpControl In Me
'Create a rectangular region with its parameters
If TmpControl.Visible = True And TmpControl.Width <> 0 Then
TmpRegn = CreateRectRgn(TmpControl.Left, TmpControl.Top,
TmpControl.Left + TmpControl.Width, TmpControl.Top + TmpControl.Height)
'Combines the regions
CombineRgn Regn, Regn, TmpRegn, RGN_XOR
End If
Next TmpControl

'Make the regions
SetWindowRgn Me.hWnd, Regn, True
DeleteObject (Regn)
DeleteObject (TmpRegn)

Fixing Window Region Memory Leak

Post by wxforecast » Sat, 23 Dec 2006 06:00:53

Nevermind. Total brain fart.

Just cleaning up the TmpRegn in the scope of the for loop fixed it up. Wow I
need some serious sleep.