Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / New Users / March 2006

Tip: Looking for answers? Try searching our database.

probable VisualStyleRenderer.GetBackgroundRegion leak

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Scott Dale Robison - 16 Mar 2006 20:39 GMT
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.
Scott Dale Robison - 21 Mar 2006 01:59 GMT
> 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

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.