GDI Objects not releaseing

  • Thread starter Thread starter Smokey Grindel
  • Start date Start date
S

Smokey Grindel

I have an application that has a system tray icon, which updates every 10
seconds to a custom made image... and every time it updates my GDI object
count increments, this shouldn't happen, any ideas on what to look for?

I am custom drawing the 16x16 image as a bitmap. It takes an existing icon,
then draws ontop of it. then assigning it to the notify icon using this
statement

ntfIcon.Icon = New Icon(Icon.FromHandle(bmp.GetHicon), New Size(16, 16))
 
Smokey said:
I have an application that has a system tray icon, which updates every 10
seconds to a custom made image... and every time it updates my GDI object
count increments, this shouldn't happen, any ideas on what to look for?

If you create it, and can Dispose of it when you're done with it, then
do so.
ntfIcon.Icon = New Icon(Icon.FromHandle(bmp.GetHicon), New Size(16, 16))

GDI objects tend to wrap up Unmanaged resources and, if you don't
explicitly Dispose of them, they'll hang around, holding onto those
precious resources, until Garbage Collection [finally] gets around to
clearing them up; which /might/ not be for hours!

Try something like this:

Dim icon as New Icon( ...

If Not ( ntfIcon Is Nothing ) Then
ntfIcon.Dispose()
ntfIcon = icon
End If

HTH,
Phill W.
 
I seem to get a Cannot access a disposed object error if I dispose the icon
directly off the notifyicon control... any ways around this?

Phill W. said:
Smokey said:
I have an application that has a system tray icon, which updates every 10
seconds to a custom made image... and every time it updates my GDI object
count increments, this shouldn't happen, any ideas on what to look for?

If you create it, and can Dispose of it when you're done with it, then do
so.
ntfIcon.Icon = New Icon(Icon.FromHandle(bmp.GetHicon), New Size(16, 16))

GDI objects tend to wrap up Unmanaged resources and, if you don't
explicitly Dispose of them, they'll hang around, holding onto those
precious resources, until Garbage Collection [finally] gets around to
clearing them up; which /might/ not be for hours!

Try something like this:

Dim icon as New Icon( ...

If Not ( ntfIcon Is Nothing ) Then
ntfIcon.Dispose()
ntfIcon = icon
End If

HTH,
Phill W.
 
I changed it to this
If ntfIcon.Icon IsNot Nothing Then

Dim Icn As Icon = ntfIcon.Icon

ntfIcon.Icon = Nothing

Icn.Dispose()

End If

isn't there a better way of doing it?

Phill W. said:
Smokey said:
I have an application that has a system tray icon, which updates every 10
seconds to a custom made image... and every time it updates my GDI object
count increments, this shouldn't happen, any ideas on what to look for?

If you create it, and can Dispose of it when you're done with it, then do
so.
ntfIcon.Icon = New Icon(Icon.FromHandle(bmp.GetHicon), New Size(16, 16))

GDI objects tend to wrap up Unmanaged resources and, if you don't
explicitly Dispose of them, they'll hang around, holding onto those
precious resources, until Garbage Collection [finally] gets around to
clearing them up; which /might/ not be for hours!

Try something like this:

Dim icon as New Icon( ...

If Not ( ntfIcon Is Nothing ) Then
ntfIcon.Dispose()
ntfIcon = icon
End If

HTH,
Phill W.
 
Smokey Grindel said:
I changed it to this
If ntfIcon.Icon IsNot Nothing Then

Dim Icn As Icon = ntfIcon.Icon

ntfIcon.Icon = Nothing

Icn.Dispose()

End If

isn't there a better way of doing it?


I did it similar in order to replace the icon (I had the same problem):

Dim OldIcon As Icon

OldIcon = NI.Icon
NI.Icon = NewIcon

If OldIcon IsNot Nothing Then
OldIcon.Dispose()
End If



Armin
 
Smokey Grindel said:
I changed it to this
If ntfIcon.Icon IsNot Nothing Then

Dim Icn As Icon = ntfIcon.Icon

ntfIcon.Icon = Nothing

Icn.Dispose()

End If

isn't there a better way of doing it?

This code is okay. You could also write

\\\
Using OldIcon As Icon = NotifyIcon1.Icon
NotifyIcon1.Icon = Nothing
End Using
///
 
Event with that my GDI Object count is increasting constantly... I am up to
4,000 objects right now and it doesn't seem to be disposing of them... not
too long till I hit the 10,000 cieling and cause a GDI crash.....
 
Back
Top