probable VisualStyleRenderer.GetBackgroundRegion leak

G

Guest

The following code leaks a ton of GDI handles despite Disposing of the
related Region objects:

private void Form1_Paint(object sender, PaintEventArgs e)
{
Region region = null;

try
{
VisualStyleRenderer renderer =
new VisualStyleRenderer(
VisualStyleElement.Button.PushButton.Normal.ClassName,
VisualStyleElement.Button.PushButton.Normal.Part,
2);

region = renderer.GetBackgroundRegion(e.Graphics,
ClientRectangle);
}
catch (Exception x)
{
timer1.Enabled = false;
Trace.WriteLine(x.ToString());
}
finally
{
if (region != null)
region.Dispose();
}
}

Am I completely brain dead and this is wrong or is it a bug as I suspect? If
I create the Region objects myself all is well and there is no leak, but if I
rely on the VisualStyleRenderer object to give me the Region it leaks like a
sieve.

Note: the aforementioned code is obviously horribly incomplete. This was the
smallest 'safe' example I could come up with however that illustrates the
problem in a paint method.

Any info or guidance would be appreciated.
 
G

Guest

Scott Dale Robison said:
The following code leaks a ton of GDI handles despite Disposing of the
related Region objects:

<snipped>

Don't everyone answer at once! :)

Anyway, I've confirmed that VisualStyleRenderer.GetBackgroundRegion does
indeed leak a GDI handle (specifically, HRGN). The workaround at this time is
to import GetThemeBackgroundRegion into your code and call it directly
(making sure to release the HRGN after calling Region.FromHrgn).

Scott Dale Robison
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top