Re: GDI Objects not releaseing

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance



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-@xxxxxxxxxxxxxxxxxxxx> wrote in message
news:ff552a$pl6$1@xxxxxxxxxxxxxxxxxxxx
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.


.



Relevant Pages

  • Re: Need explanation on calling Close() from finalizer.
    ... understand is why .NET classes cannot clean up their unmanaged resources ... > NET's Garbage Collector, like you've stated, automated memory management, ... > the Dispose pattern. ... >>> hoping your finalizer will someday be called when the garbage collector ...
    (microsoft.public.dotnet.framework.adonet)
  • Re: " //Clean Up managed resources " f&*ck
    ... time (such as releasing internal unmanaged resources). ... Since Dispose ... methods is responsible for cleaning resources, the finalizer code ... it is about 'managed resource'. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Finalize and database connection... contradiction in msdn or a
    ... which, from what I read, is correct but it is recommended that you dispose of ... advocate disposing of them rather than relying on the Finalize method. ... > 'considered as a managed object since it's an instance of a class provided ... > If your database connection object uses any unmanaged resources then it ...
    (microsoft.public.dotnet.languages.vb)
  • Re: Clean Up managed resources
    ... these have unmanaged resources. ... If somebody called Dispose on your object, ... If the finalizer is called on your object, ... threads while it determines reachability from all the different roots. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Implementing IDisposible on light objects
    ... neither unmanaged resources nor managed resources which are resource ... virtual void Dispose(bool disposing) method, ... Calling Dispose on an object whose class implements IDisposable does not ... Only objects which implement finalizers are subject to finalization. ...
    (microsoft.public.dotnet.languages.csharp)