473,387 Members | 1,455 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Discover not disposed objects

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

--
Cheers,
Crirus

------------------------------
If work were a good thing, the boss would take it all from you

------------------------------
Nov 20 '05 #1
7 2075
* "Crirus" <Cr****@datagroup.ro> scripsit:
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...

--
Herfried K. Wagner [MVP]
<http://www.mvps.org/dotnet>
Nov 20 '05 #2
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/de...izeDispose.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/d...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
"Crirus" <Cr****@datagroup.ro> wrote in message
news:OF**************@TK2MSFTNGP12.phx.gbl... 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:))

--
Cheers,
Crirus

------------------------------
If work were a good thing, the boss would take it all from you

------------------------------

Nov 20 '05 #3
In article <OF**************@TK2MSFTNGP12.phx.gbl>, Crirus wrote:
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 Shelton
MVP [Visual Basic]
Nov 20 '05 #4
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/de...izeDispose.asp

Just a thought
Jay

"Tom Shelton" <to*@mtogden.com> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
In article <OF**************@TK2MSFTNGP12.phx.gbl>, Crirus wrote:
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 Shelton
MVP [Visual Basic]

Nov 20 '05 #5
In article <OY**************@TK2MSFTNGP12.phx.gbl>, Jay B. Harlow [MVP - Outlook] wrote:
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/de...izeDispose.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 :)

--
Tom Shelton
MVP [Visual Basic]
Nov 20 '05 #6
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

--
Cheers,
Crirus

------------------------------
If work were a good thing, the boss would take it all from you

------------------------------

"Tom Shelton" <to*@mtogden.com> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
In article <OF**************@TK2MSFTNGP12.phx.gbl>, Crirus wrote:
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 Shelton
MVP [Visual Basic]

Nov 20 '05 #7
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

"Tom Shelton" <to*@mtogden.com> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
In article <OY**************@TK2MSFTNGP12.phx.gbl>, Jay B. Harlow [MVP -

Outlook] wrote:
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/de...izeDispose.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 :)

--
Tom Shelton
MVP [Visual Basic]

Nov 20 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Einar Høst | last post by:
Well, subject says it all really... Thanks, Einar -- "If it was so, it might be; and if it were so, it would be; but as it isn't, it ain't. That's logic" -- Lewis Carroll
7
by: Ryan Park | last post by:
Hi, //SITUATION I got a panel control that hold a certain position on a form. Every controls or UIs are on this panel. At certain situation, I called dispose() method of this panel control...
9
by: David Sworder | last post by:
Hi, I have a form that displays data (is that vague enough for you?). The data comes in on a thread-pool thread. Since the thread pool thread is not the same as the UI thread, the callback...
0
by: Richard Blewett [DevelopMentor] | last post by:
In theory there is nothing wrong with the Windows Forms part of your application (using your code as a start I mocked up the app and can happily press the connect and disconnect buttons...
6
by: Jim H | last post by:
I have a class object that creates and uses a System.Threading.Timer. In the destructor for my class I cancel the timer using timer.Change(infinite, infinite) then I call timer.Dispose() to clean...
4
by: wirawan | last post by:
hi all i had some problems and need your sugestion to solve it. why event of controls that i have been disposed still alive and trigger method had been bind to it. best regards wirawan --
6
by: SamSpade | last post by:
Public Function PicCreateGraphics() As Graphics 'Client should dispose this PicCreateGraphics = Graphics.FromImage(mDocumentImage) mPicCreateGraphicsSaved = PicCreateGraphics 'Saved so I can...
3
by: dast | last post by:
Hi, I'm using a delegate to edit some controls on formA from another thread on formB. This works nicely the first time I run formA. But if I close formA and reopen it I get errors. When...
1
by: Daniel | last post by:
is a reader automaticaly disposed if the connection it is associated with is closed? what will happen if an app: while true open connection open reader close connection end while
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.