By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,970 Members | 1,669 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,970 IT Pros & Developers. It's quick & easy.

GDI Objects not releaseing

P: n/a
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))
Oct 17 '07 #1
Share this Question
Share on Google+
5 Replies


P: n/a
Smokey Grindel wrote:
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.
Oct 17 '07 #2

P: n/a
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." <p-.-a-.-w-a-r-d-@-o-p-e-n-.-a-c-.-u-kwrote in message
news:ff**********@south.jnrs.ja.net...
Smokey Grindel wrote:
>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.

Oct 17 '07 #3

P: n/a
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." <p-.-a-.-w-a-r-d-@-o-p-e-n-.-a-c-.-u-kwrote in message
news:ff**********@south.jnrs.ja.net...
Smokey Grindel wrote:
>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.

Oct 17 '07 #4

P: n/a
"Smokey Grindel" <no****@nospam.comschrieb:
>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
///

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>
Oct 17 '07 #5

P: n/a
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.....

"Herfried K. Wagner [MVP]" <hi***************@gmx.atwrote in message
news:Op**************@TK2MSFTNGP04.phx.gbl...
"Smokey Grindel" <no****@nospam.comschrieb:
>>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
///

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>

Oct 17 '07 #6

This discussion thread is closed

Replies have been disabled for this discussion.