Results 1 to 7 of 7

Thread: NotifyIcon GDI Leak

  1. #1

    Thread Starter
    New Member
    Join Date
    Dec 2003
    Posts
    14

    NotifyIcon GDI Leak

    I have the following code:

    VB Code:
    1. Private Sub UpdateIcon(ByVal txt As String, ByVal col As Color)
    2.         Dim bmp As New Bitmap(16, 16)
    3.         Dim G As Graphics = Graphics.FromImage(bmp)
    4.         Dim oIcon As Icon
    5.         G.Clear(Color.FromArgb(cserver.BackgroundIcon))
    6.         G.DrawString(txt, New Font("Tahoma", 9, FontStyle.Bold), New SolidBrush(col), -2, 0)
    7.         oIcon = Icon.FromHandle(bmp.GetHicon)
    8.         StatusIcon.Icon = oIcon
    9.  
    10.     End Sub


    Problem is that every time the function is called, the StatusIcon.Icon call leaks 2 GDI hanldes.

    Is there an alternative implementation?
    I checked MSDN and apparently this was fixed in Framework 1.0 SP2 for Form.Icon. I can't find a reference anywhere to NotifyIcon GDI Leaks.

    Help! I'm comletely stuck on this!

    Cheers,
    Cheers,

    Black Thunder

  2. #2

    Thread Starter
    New Member
    Join Date
    Dec 2003
    Posts
    14
    I'm running Visual Studio 2003 with 1.1 Framework under XP SP1

    I've just tested the same code bug assigned the icon to the form instead. It still leaks GDI handles.

    Cheers
    Cheers,

    Black Thunder

  3. #3

    Thread Starter
    New Member
    Join Date
    Dec 2003
    Posts
    14
    *bump*

    nobody know solution to this?
    Cheers,

    Black Thunder

  4. #4
    You need to manually destroy it using the DestroyIcon api call.
    VB Code:
    1. Private Declare Function DestroyIcon Lib "user32.dll" (hIcon As IntPtr) As Integer
    And then change your method:
    VB Code:
    1. Dim hIcon As IntPtr = bmp.GetHicon()
    2. StatusIcon.Icon = Icon.FromHandle(hIcon)
    3.  
    4. ' Destroy icon using windows api
    5. DestroyIcon(hIcon)
    This is because the GetHicon method creates a new GDI HICON resource that (like all GDI resources) must be destroyed manually. Unfortunately, the framework does not include a method to do this, so you have to resort to an API call.


    Best regards,
    Andrew Young
    Skybound Software

  5. #5
    Fanatic Member pax's Avatar
    Join Date
    Mar 2001
    Location
    Denmark
    Posts
    840
    Disposing the bitmap and graphics objects might help too.

    Add this to the end of the sub.
    VB Code:
    1. bmp.Dispose
    2. G.Dispose
    I wish I could think of something witty to put in my sig...

    ...Currently using VS2013...

  6. #6

    Thread Starter
    New Member
    Join Date
    Dec 2003
    Posts
    14
    DestroyIcon worked like a charm. Thanks a lot.

    Is this a VB.NET bug or a documented feature?


    Cheers,
    Cheers,

    Black Thunder

  7. #7
    It's a little of both. The behavior is by design; the problem is that it's not well documented, and therefore it seems like a bug.

    All the GDI interop methods (GetHfont, GetHbitmap, GetHicon, GetHrgn) require the resouce they create to be manually destroyed by your program, although none of them mention this in the MSDN documentation. When you think about it, though, it would be impossible for the framework to destroy these resources for you automatically, since they're just IntPtrs.
    Best regards,
    Andrew Young
    Skybound Software

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width