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
------------------------------ 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>
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
------------------------------
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]
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]
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]
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]
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] This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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
|
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...
|
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...
|
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...
|
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...
|
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
--
|
by: SamSpade |
last post by:
Public Function PicCreateGraphics() As Graphics
'Client should dispose this
PicCreateGraphics = Graphics.FromImage(mDocumentImage)
mPicCreateGraphicsSaved = PicCreateGraphics 'Saved so I can...
|
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...
|
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
|
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,...
|
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$) {
}
...
|
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...
|
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...
|
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
|
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,...
|
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...
|
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,...
|
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...
| |