Discover not disposed objects

  • Thread starter Thread starter Crirus
  • Start date Start date
C

Crirus

Is there a way to catch undisposed objects that only occupy memory, in order
to call dispose for them?
I may be forgetting to dispose fonts, pens, graphics sometimes... how to
find them (without reading the whole code again:))
 
* "Crirus said:
Is there a way to catch undisposed objects that only occupy memory, in order
to call dispose for them?
I may be forgetting to dispose fonts, pens, graphics sometimes... how to
find them (without reading the whole code again:))

I don't know if there is a tool to "automate" that, but keep the code
compact, structure it in procedures -- so you won't "forget" to
dispose...
 
Crirus,
When we get Whidbey (VB.NET 2004 later in 2004) we will have a Using
statement that will call Dispose automatically for us!

Until then:
Is there a way to catch undisposed objects that only occupy memory, in order
to call dispose for them?
With out a reference to the said object, you will not be able to call the
Dispose method. Normally classes that implement Dispose also implement the
Finalize method. The Dispose method will disable calling Finalize, while the
Finalize method will call the Dispose method...

http://msdn.microsoft.com/library/d.../en-us/cpgenref/html/cpconFinalizeDispose.asp

If a class implements the Finalize method, then the GC will eventually call
the Finalize method. Hence the GC should eventually call the Dispose
method...

You can use the CLR profiler to help identify objects that need to be
finalize, this may lead you to the objects that need to be Disposed of.

http://www.microsoft.com/downloads/...52-D7F4-4AEB-9B7A-94635BEEBDDA&displaylang=en

I believe you could use one of the many Code Critics/Code Analyzers out
there to identify objects that have not been disposed:

Such as:
http://www.fmsinc.com/dotnet/analyzer/

At least I would expect a Code Critic/Analyzer to report objects that were
not disposed!

Hope this helps
Jay
 
Is there a way to catch undisposed objects that only occupy memory, in order
to call dispose for them?
I may be forgetting to dispose fonts, pens, graphics sometimes... how to
find them (without reading the whole code again:))

There is no way, once you've released all references...

For custom classes, though it was decided over on the C# group that the
best solution is to throw an exception from your finalize method. Sure,
it brings your program down - but you know when you forgot to dispose of
an object :)
 
Tom,
Throwing an exception in your Finalize method is a good idea!

As long as you implement the Disposable Finalize pattern correctly ;-)

http://msdn.microsoft.com/library/d.../en-us/cpgenref/html/cpconFinalizeDispose.asp

Just a thought
Jay

Yeah, I of course should have mentioned that - but I guess I assumed
that was understood :)

Actually, I thought it was pretty clever myself and am starting to use
that it in my own objects. This way you always know when Finalize has
been called :)
 
Wel, my concerns are related to pens, graphics, brushes..etc... the graphics
stuffs..that ones I use most.. my own classes are not to me dispasable as
long as application run
 
Tom,
An alternative to throwing an exception is to use Debug.Fail.

I will use Debug.Fail when I want the message in the debugger, however I do
not want the message in the release build.

As technically not calling Dispose is not an error.

Just a thought
Jay

 
Back
Top