GDI Objects not releaseing

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))
 
P

Phill W.

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.
 
S

Smokey Grindel

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.
 
S

Smokey Grindel

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.
 
A

Armin Zingler

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
 
H

Herfried K. Wagner [MVP]

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
///
 
S

Smokey Grindel

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.....
 

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