|
-
Jul 23rd, 2004, 07:19 AM
#1
Thread Starter
New Member
NotifyIcon GDI Leak
I have the following code:
VB Code:
Private Sub UpdateIcon(ByVal txt As String, ByVal col As Color)
Dim bmp As New Bitmap(16, 16)
Dim G As Graphics = Graphics.FromImage(bmp)
Dim oIcon As Icon
G.Clear(Color.FromArgb(cserver.BackgroundIcon))
G.DrawString(txt, New Font("Tahoma", 9, FontStyle.Bold), New SolidBrush(col), -2, 0)
oIcon = Icon.FromHandle(bmp.GetHicon)
StatusIcon.Icon = oIcon
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,
-
Jul 23rd, 2004, 07:23 AM
#2
Thread Starter
New Member
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
-
Jul 24th, 2004, 11:21 PM
#3
Thread Starter
New Member
*bump*
nobody know solution to this?
-
Jul 25th, 2004, 07:05 PM
#4
New Member
You need to manually destroy it using the DestroyIcon api call.
VB Code:
Private Declare Function DestroyIcon Lib "user32.dll" (hIcon As IntPtr) As Integer
And then change your method:
VB Code:
Dim hIcon As IntPtr = bmp.GetHicon()
StatusIcon.Icon = Icon.FromHandle(hIcon)
' Destroy icon using windows api
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
-
Jul 26th, 2004, 01:23 AM
#5
Disposing the bitmap and graphics objects might help too.
Add this to the end of the sub.
I wish I could think of something witty to put in my sig...
...Currently using VS2013...
-
Jul 26th, 2004, 07:04 AM
#6
Thread Starter
New Member
DestroyIcon worked like a charm. Thanks a lot.
Is this a VB.NET bug or a documented feature?
Cheers,
-
Jul 26th, 2004, 07:31 AM
#7
New Member
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|