Hi Everyone,
I've read most of the posts regarding "Dispose" in this NG.
I still have some questions.
If I declare and use a disposable object (say a DB connection) within
a method, is it worth calling Dispose() within my "finally" block,
even if that object only has local scope?
When the thread exits this method, won't the GC collect my disposable
object regardless of whether I call Dispose() or not?
What would be the difference between calling Dispose() here and not
calling Dispose()?
Thanks for clearing my confusion...
Wobbles 14 6723
wobbles <no************ *************** @nospam.hotmail .com> wrote: I've read most of the posts regarding "Dispose" in this NG. I still have some questions.
If I declare and use a disposable object (say a DB connection) within a method, is it worth calling Dispose() within my "finally" block, even if that object only has local scope?
Absolutely - although I'd use a using() block.
When the thread exits this method, won't the GC collect my disposable object regardless of whether I call Dispose() or not?
No. The GC doesn't run at the end of every method - it runs when it
feels it needs to.
What would be the difference between calling Dispose() here and not calling Dispose()?
Timing - if you call Dispose(), all the appropriate cleaning up will be
done by the time the call returns, rather than at some indeterminate
point in the future.
--
Jon Skeet - <sk***@pobox.co m> http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Disposable objects are objects that typicall use resources that the GC is
slow to reclaim or may not reclaim at all. By calling Dispose() you let
the object know that it should release it's resources. It will do so, and
wait to be eaten by GC.
If you don't call Dispose() you run the risk of running out of resources
(GC isn't very good at cleaning up those).
--
Using M2, Opera's revolutionary e-mail client: http://www.opera.com/m2/
For a laugh, try web browsing with Opera's User Mode and Nostalgia enabled
wobbles wrote: If I declare and use a disposable object (say a DB connection) within a method, is it worth calling Dispose() within my "finally" block, even if that object only has local scope?
Yes. You should ALWAYS call Dispose if you do not need this object anymore.
When the thread exits this method, won't the GC collect my disposable object regardless of whether I call Dispose() or not?
GC only calls finalizers, not Dipose()...
The object should implement a finilizer if this situation is allowed.
Some objects have the IDisposable-Interface but they are not implemeting a
Finalizer.
So if you do not call Dispose the resourse will never be freed (maybe at
program termination)
--
Greetings
Jochen
Do you need a memory-leak finder ? http://www.codeproject.com/tools/leakfinder.asp
And the finallizer normally calls dispose (except not the unmanaged path if
you follow the normal pattern for IDisposable)
"Jochen Kalmbach" <no************ ********@holzma .de> wrote in message
news:Xn******** *************** **********@127. 0.0.1... wobbles wrote:
If I declare and use a disposable object (say a DB connection) within a method, is it worth calling Dispose() within my "finally" block, even if that object only has local scope? Yes. You should ALWAYS call Dispose if you do not need this object
anymore.
When the thread exits this method, won't the GC collect my disposable object regardless of whether I call Dispose() or not?
GC only calls finalizers, not Dipose()...
The object should implement a finilizer if this situation is allowed. Some objects have the IDisposable-Interface but they are not implemeting a Finalizer.
So if you do not call Dispose the resourse will never be freed (maybe at program termination)
-- Greetings Jochen
Do you need a memory-leak finder ? http://www.codeproject.com/tools/leakfinder.asp
<snip> If I declare and use a disposable object (say a DB connection) within a method, is it worth calling Dispose() within my "finally" block, even if that object only has local scope?
Absolutely - although I'd use a using() block.
I understand that the "using()" block replaces "try" and "finally" but
how do I implement a catch to handle exceptions when I use "using"? When the thread exits this method, won't the GC collect my disposable object regardless of whether I call Dispose() or not?
No. The GC doesn't run at the end of every method - it runs when it feels it needs to.
Ah, I wasn't sure about this. Thanks for clearing it up.
I thought that exiting the method would be a trigger for the GC.
Obviously not. What would be the difference between calling Dispose() here and not calling Dispose()?
Timing - if you call Dispose(), all the appropriate cleaning up will be done by the time the call returns, rather than at some indeterminate point in the future.
Thanks Jon.
Thanks for answering my questions. I've read all replies and they've all been
useful.
I have one more question:
I had thought of implementing the IDisposable interface in my class but there
are no class-level variables that need disposing. I only have declarations of
references to disposable objects within methods.
Should I implement IDisposable? *What* would I dispose when I override Dispose()
(considering my disposable object is in a method)?
Say I have:
public class Wobbles {
public Wobbles {}
public void DoThings() {
DisposableObjec t o = null;
try {
o = new DisposableObjec t();
o.Dosomething() ;
}catch (Exception e) {
//catch exceptions
}finally {
if (o!=null) o.Dispose();
}
}
}
wobbles <no************ *************** @nospam.hotmail .com> wrote: Absolutely - although I'd use a using() block.
I understand that the "using()" block replaces "try" and "finally" but how do I implement a catch to handle exceptions when I use "using"?
I would personally either wrap the using block in a try/catch, or wrap
the try/catch in a using block. I'd prefer that to doing the finally
block explicitly, just for easy maintenance.
<snip>
--
Jon Skeet - <sk***@pobox.co m> http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
wobbles,
If you need to catch exceptions, then in this case, do not use "using".
Use a try/catch/finally block and make sure you declare your variables
outside of the block and initialized to null (initialize in the block).
Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m
"wobbles" <no************ *************** @nospam.hotmail .com> wrote in
message news:v9******** *************** *********@4ax.c om... <snip>
If I declare and use a disposable object (say a DB connection) within a method, is it worth calling Dispose() within my "finally" block, even if that object only has local scope?
Absolutely - although I'd use a using() block.
I understand that the "using()" block replaces "try" and "finally" but how do I implement a catch to handle exceptions when I use "using"?
When the thread exits this method, won't the GC collect my disposable object regardless of whether I call Dispose() or not?
No. The GC doesn't run at the end of every method - it runs when it feels it needs to.
Ah, I wasn't sure about this. Thanks for clearing it up. I thought that exiting the method would be a trigger for the GC. Obviously not.
What would be the difference between calling Dispose() here and not calling Dispose()?
Timing - if you call Dispose(), all the appropriate cleaning up will be done by the time the call returns, rather than at some indeterminate point in the future.
Thanks Jon.
Nicholas Paldino [.NET/C# MVP] <mv*@spam.guard .caspershouse.c om> wrote: If you need to catch exceptions, then in this case, do not use "using". Use a try/catch/finally block and make sure you declare your variables outside of the block and initialized to null (initialize in the block).
I don't like doing that as it "pollutes" the variable namespace. I'd
rather use:
try
{
using (...)
{
}
}
catch (...)
{
}
or
using (...)
{
try
{
}
catch (...)
}
personally. It's then easier to make sure you're cleaning things up,
'cos you only need to check that your using block is there.
Just personal preference though.
--
Jon Skeet - <sk***@pobox.co m> http://www.pobox.com/~skeet
If replying to the group, please do not mail me too This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: David N |
last post by:
Hi All,
I just wonder if in C#, I can develop a user defined control that can call
its parent function which is not yet developed. For example, how do I make
my user control call a to-be-developed-function cmdOkay_Click() function as
described below.
1. Create an user control that contains an OK button as below
Public Class MyButton:System.Windows.Form.UserControl
|
by: Jazper |
last post by:
hi
i have this problem. i made a class deverted by CRootItem with
implementation of IDisposable-Interface. i made a test-funktion to test my
Dispose-Method.... but when set a breakpoint in my Dispose-Method and call
the GC nothing happend!!! my Disposemethod has never been called!! so the GC
dont call my Dispose-Method although I implemented IDisposable?
what am i doing wrong?
|
by: Clint |
last post by:
Hey all -
I'm having a really confusing problem concerning a web service. Right
now, I have an application that needs to call a web service that does
nothing but return "true" (this will obviously change once the
program's fully built to actually do something, but for testing, it
works).
The only code I added to the service is below:
|
by: jon |
last post by:
How can I call a base interface method?
class ThirdPartyClass :IDisposable { //I can not modify this class
void IDisposable.Dispose() {
Console.WriteLine( "ThirdPartyClass Dispose" );
}
}
class MyClass :ThirdPartyClass, IDisposable {
void IDisposable.Dispose() {
|
by: Giovanni Bassi |
last post by:
Hello Group,
I am running an operation in a different thread. There are resources that
are released when the thread is done running. This is done at the end of the
execution as it raises an event, and then the operation handling this event
calls threaded object's dispose method.
The problem is: If an exception is thrown the event is never raised, the
operation never executes dispose and my resources get stuck on the memory
until the app...
| |
by: Zytan |
last post by:
I have a log class that makes a synchronized TextWriter like so, in
the constructor:
StreamWriter sw = new StreamWriter(filename);
tw = TextWriter.Synchronized(sw);
In the destructor, ~MyLogClass(), I call:
tw.WriteLine("some stuff");
tw.Close();
|
by: Rudi |
last post by:
Hello,
following problem: At program end or release an assembly a serial device
should get a final exit sequence. How can I do this?
With Dispose() it's no problem, but this assembly is used in a com
interop dll and it must be guaranteed, that the final sequence is send
to the serial device, when the calling application dont send Close() or
Dispose().
|
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
| |
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |