Is it safe to create and use a disposable object inline? - c#

I have seen so many times developers using a disposable object inline, here for instance. By inline I mean:
var result = new DataTable().Compute("1 + 4 * 7", null);
I know that the Dispose method won't be called, but since no reference is held to the object, how will the garbage collector handle it? Is it safe to use a disposable object like that?
I used a DataTable in my example because it is the only concrete example I found, but my question applies to disposable objects in general. I do not personally use them like that, I just wanted to know if they are handled diffently by the GC if they are used that way.

The key problem here is the timing when Dispose is called, which is just unknown in your example (providing a good implementation of IDisposable — see below). I'd consider using an IDisposable instance without a using statement a code smell. The class is implementing IDisposable for a reason, and thus you as a user should obey its contract.
However, note that in a correct implementation of IDisposable the class'es finalizer handles the disposal of an un-disposed object. Hence even if not instantiated within a using, the disposal shall be performed, but in unknown time and on a different (the GC's) thread.
I just wanted to know if they are handled diffently by the GC if they are used that way.
No, the GC treats all object alike and doesn't treat IDisposable implementations anyhow differently. However, in a correct implemenetation of IDisposable the Finalize method (invoked by the GC on every object unless suppressed on a per-object basis) call would lead to invoking the Dispose.

If the object that is being initialized inline has a destructor implemented, that destructor could be calling Dispose() after the object has gone out of scope.
However, the cleaner and correct way is to use a using statement because this could potentially lead to just instances of objects hanging around with no purpose.

It is not safe to use disposable objects in this way. Yes, no reference wont be held but the purpose of Disposable pattern is to dispose all unmanaged resources(like OS handles) which cannot be garbage collected.

Related

Difference between finalizer and finally in C# [duplicate]

I am studying how garbage collector works in c#. I am confused over the use of Destructor, Dispose and Finalize methods.
As per my research and understandings, having a Destructor method within my class will tell the garbage collector to perform the garbage collection in the way mentioned in the destructor method which cannot be called explicitly on the instances of the class.
The Dispose method is meant to provide the user to control the garbage collection. The Finalize method frees the resources used by the class, but not the object itself.
I am not sure if I understand it the right way. Please clarify the doubts. Any further links or guides are welcome.
Destructor implicitly calls the Finalize method, they are technically the same. Dispose is available with objects that implement the IDisposable interface.
You may see : Destructors C# - MSDN
The destructor implicitly calls Finalize on the base class of the
object.
Example from the same link:
class Car
{
~Car() // destructor
{
// cleanup statements...
}
}
The Destructor's code is implicitly translated to the following code:
protected override void Finalize()
{
try
{
// Cleanup statements...
}
finally
{
base.Finalize();
}
}
Your understanding for the Destructor is right:
From MSDN
The programmer has no control over when the destructor is called
because this is determined by the garbage collector. The garbage
collector checks for objects that are no longer being used by the
application. If it considers an object eligible for destruction, it
calls the destructor (if any) and reclaims the memory used to store
the object. Destructors are also called when the program exits. It is
possible to force garbage collection by calling Collect, but most of
the time, this should be avoided because it may create performance
issues.
In C# terms, a destructor and finalizer are basically interchangeable concepts, and should be used to release unmanaged resources when a type is collected, for example external handles. It is very rare that you need to write a finalizer.
The problem with that is that GC is non-deterministic, so the Dispose() method (via IDisposable) makes it possible to support deterministic cleanup. This is unrelated to garbage collection, and allows the caller to release any resources sooner. It is also suitable for use with managed resources (in addition to unmanaged), for example if you have a type that encapsulates (say) a database connection, you might want disposing of the type to release the connection too.

When shouldn't I implement IDisposable?

Firstly, sorry if this is considered a duplicate - I know it is a common topic, but I have looked and not found a satisfactory answer.
There are a lot of questions asking when to use IDisposable, but from everything I've read, I just can't see why you wouldn't implement it in every class you make. What is there to lose? Does it have a big performance hit?
Part of my understanding of IDisposable is that one of the things it does is:
Dispose()ing other IDisposables owned by the object. Forgive my ignorance, but does this apply only to fields/properties of the given object, or does it also extend to objects created within its methods too?
For instance, if a Font was created inside a method within a class that implements IDisposable, but that Font wasn't initialised with a using block, or .Dispose()d explicitly at the end of the method; would it be disposed when its IDisposable parent/class was GCd/disposed? Or otherwise, would the Font never be disposed?
I don't mean to digress, but if it is true that it would act as a 'catch all' like this (effectively disposing of any erroneous child IDisposable objects that would otherwise be left undisposed), isn't that reason alone enough to justify always implementing IDisposable whenever possible?
The rule is very simple, you need to implement IDisposable if you have any fields in your class that are of a type that is disposable. So that you can dispose them.
What happens inside a method has little to do with the fields of your class. If you create the font and store it in a field then yes, the above rule says that you need a Dispose() method. If you don't but just use the font to draw something, like you normally do, then always use the using statement so you immediately dispose the font after you are done using it.
Most objects don't need it. The framework takes good care of Garbage Collection. COM objects and Graphics objects are among the ones that do and should implement IDisposable for good clean up. Yes there may be some inherent performance loss when recycling objects.
An object should implement IDisposable if it will know of things that need to happen sometime before the end of the universe, and nothing else is going have the knowledge and impetus necessary to ensure that those things get done; the Dispose method lets the object know that it had better do those things immediately, because otherwise they'll likely never get done.
An abstract type or interface should implement IDisposable if it is likely that instances of a derived or implementing that type might know of things that need to happen sometime before the end of the universe, and the last entity holding a reference is apt to know that it's an instance of something derived from or implementing the abstract or interface type, rather than as something which implements a more specific type that implements IDisposable.
A type which implements an interface that inherits IDisposable will be required to implement IDisposable, whether or not it would have any other reason for doing so.
Types which have reason to implement IDisposable should do so. Types which don't, shouldn't.
ADDENDUM
To understand why one shouldn't always implement IDisposable, it may be helpful to consider the real cost of doing so. The amount of time required for the processor to call a do-nothing Dispose method is trivial, but that isn't the real consideration. The bigger issue comes when one considers that most objects fit one of four descriptions:
Objects which encapsulate resources, and need to have one clearly-defined owner
Objects which encapsulate mutable state, and need to have one clearly-defined owner.
Objects of immutable types, which do not need to have clearly-defined owners.
Instances of mutable types which will never be exposed to code that could mutate them, and do not need to have clearly-defined owners.
Correct use of mutable objects generally requires keeping track of who owns them, as is the case with objects that hold resources. Likewise, if an object with mutable state is used to encapsulate mutable state for another object, proper use of the latter object will require keeping track of who owns it, as is the case with objects encapsulating other objects that own resources. The difference is between objects with resources and those with mutable state is that when an instance of a mutable type is used to encapsulate the state of an immutable object (ensuring the instance is never exposed to code that would mutate it), it will no longer be necessary to keep track of who owns it. By contrast, it's necessary to track ownership of objects which encapsulate other objects that hold resources, even if those objects are semantically immutable.

Should IDisposable.Dispose() implementations be idempotent?

The Microsoft.NET framework provides the IDisposable interface which requires an implementation of void Dispose() method. Its purpose is to enable manual, or scope-based releasing of expensive resources an IDisposable implementation may have allocated. Examples include database collections, streams and handles.
My question is, should the implementation of the Dispose() method be idempotent - when called more than once on the same instance, the instance to be 'disposed of' only once, and subsequent calls not to throw exceptions. In Java, most of the objects that have similar behavior (again streams and database connections come to my mind as examples) are idempotent for their close() operation, which happens to be the analogue for the Dispose() method.
However, my personal experience with .NET (and Windows Forms in particular), shows that not all implementations (that are part of the .NET framework itself) are idempotent, so that subsequent calls to these throw an ObjectDisposedException. This really confuses me on how a disposable object's implementation should be approached. Is there a common answer for the scenario, or is it dependent on the concrete context of the object and its usage?
should the implementation of the Dispose() method be idempotent
Yes, it should. There is no telling how many times it will be called.
From Implementing a Dispose Method on MSDN:
a Dispose method should be callable multiple times without throwing an exception.
An object with a good implementation of IDispose will have a boolean field flag indicating if it has been disposed of already and on subsequent calls do nothing (as it was already disposed).
Yes, also make sure the other methods of the class respond correctly when they are called when the object has already been disposed.
public void SomeMethod()
{
if(_disposed)
{
throw new ObjectDisposedException();
}
else
{
// ...
}
}
From MSDN:
Allow a Dispose method to be called more than once without throwing an exception. The method should do nothing after the first call.
Personally - Yes - I always make Dispose() idempotent.
During the usual life-cyle of an object in a given application it may not be necessary - the life-cyle from creation to disposal may be deterministic and well known.
However, equally, in some applications it might not be so clear.
For example, in a decorator scenario: I may have a disposable object A, decorated by another disposable object B. I may want to explicitly dispose A, and yet Dispose on B may also dispose the instance it wraps (think: streams).
Given it is relatively easy to make Dispose idempotent (ie if already disposed, do nothing), it seems silly not to.

Will object with reference kept be disposed?

May I know if the "objectToBeDisposed" will be disposed if its reference are kept by the "useDisposableObject"?
using(IDisposable objectToBeDisposed = new ObjectImplementedIDisposable ()){
ChildObject useDisposableObject = new ChildObject(objectToBeDisposed);
.... doing stuff with useDisposableObject;
}
Yes, in your example it will be disposed, because the using block explicitly calls the Dispose() method when leaving that block.
As an aside the referencing object is within the using block scope and will go out of scope at the end of the using block, which will avoid the potential issue of accessing a disposed object if the referencing object tried to use it once it was disposed.
Update thanks to #dlev
Disposal, finalization and memory deallocation are 3 totally different things in the .NET world.
Disposal is only an "application logic" thing, it is implemented through the disposable pattern.
If you implement the interface IDisposable, your object can be used with the using keyword.
The using keyword will translate in a simple way:
using (IDisposable xxx = new MyObject())
{
....
}
means
{
IDisposable xxx = new MyObject();
try
{
....
}
finally
{
if (xxx != null)
xxx.Dispose();
}
}
There is no other magic in this. That's all.
Object finalization is instead called by garbage collector when the objects should be freed.
The time when it happens and the order between destructors is unpredictable.
class myclass { ~myclass() { this code will be called by garbage collector, i cannot call it } }
The syntax is very similar to C++ destructors but the meaning is a lot different.
And then memory deallocation.
Memory deallocation is not connected to object disposal!
You can keep forever a reference to a disposed object. Disposal is just something related to application logic.
Destructors can also be cancelled or called more than once by garbage collector (see GC.ReRegisterForFinalize and GC.SuppressFinalize).
The presence of a destructor in a class slow down a lot the garbage collector, and inside destructor, you can do almost nothing except releasing unmanaged resources.
Disposal instead as I said is an application thing, is just a method, and is not related to garbage collector.
"Disposing" an object has nothing to do with which references are kept around. IDisposable itself is just an interface, nothing more.
When a class implements that interface, a user of the class can call a public void Dispose() method on the object. When the user of the class places an instance of the class in a using block, the Dispose() method will automatically get called. When an Enumerator class implements IDisposable and is used in a foreach loop, it will also have Dispose() called on it.
This has absolutely nothing to do with which references are kept around, or the automatic garbage collection system.
However, IDisposable also has a design "contract" that goes with it. This contract is a pattern that classes that implement IDisposable must follow to have correct program behavior.
The contract is that once you've called Dispose(), you should never call any property or method on the object again, otherwise you should expect an ObjectDisposedException to be thrown. The only thing that is allowed to be called on that object after that point is Dispose() again, or the object finalizer (via garbage collection).
Because of this, people will sometimes set an reference to an IDisposable class to null after they have disposed it, so they don't accidentally call methods on the already-disposed object. If you placed the reference in a using block, you don't have to worry about this; The compiler won't even let you touch the reference again. If you try, you will get an error and your program will fail to compile.
This contract is not built into the language or CLR itself - it must manually be adhered to by the implementer of a class.
Yes, it will be. I might be wrong, but I don't think #John Weldon's explanation is not accurate though. You can only call using on an IDisposable implementing class, and when the using exits, it will call Dispose() on the object.
The part where I don't agree is the 'because the referencing object is within the using block scope and will go out of scope at the end of the using block' part. The objects declared in scope will not always be disposed of when the using exits. If you take this example
JustAClass myInstance = new JustAClass();
using ( MyDisposableClass theUsingInstance = new MyDisposableClass() )
{
AnotherClass innerInstance = new AnotherClass();
myInstance.ItsOtherClass = innerInstance;
myInstance.ItsWhatever = theUsingInstance;
}
At the end of this code, myInstance.ItsOtherClass is not null. innerInstance has been created in the using section and won't be garbage collected since there is still a referenced. However, the object refered by myInstance.ItsWhatever has been disposed of, since it's the using target.

C# Using Statement and iDisposable

I have just discovered that best practise instructs that where any type implement iDisposable, then you should wrap that in a using statement to ensure the object is disposed of correctly, even in the event of an exception.
My question is, how can you easily tell which objects implement iDisposable? Or should I just wrap everything that I am unsure about in the using statement and then rely on the compiler to tell me at compile time?
Thanks.
You could ...
Look for the presence of a Dispose member
Look at the definition of your type (F12)
Do as you suggest, wrap in a using and see what the compiler says
Although, the best thing is to learn what IDisposable is used for, soon you will understand the types that do and should implement this interface. i.e. external resources, unmanaged type wrappers (GDI graphics objects for example), limited resources (database connections)
IDisposable is implemented for example by objects that give access to unmanaged or expensive resources, like files, database connections and things like that. So to a certain extent, you can guess. For the rest, intellisense tells you if the Dispose() method is available on the object.
how can you easily tell which objects implement iDisposable?
Programatically one can use.
IDisposable disposable = obj as IDisposable;
if(disposable!=null)
{
//this object implements IDisposable
}
else
{
//Not implement IDisposable interface
}
If it's a standard class, then the MSDN documentation page should say if it implements IDisposable or not. Third-party libraries also usually come with documentation. Otherwise, if you're using an IDE like Visual Studio, you can inspect the class (F12 key) and see what interfaces it implements.
If you right click and choose Goto Declaration you should get the object browser there you can se all interfaces implemented by the class.
Otherwise use the intellisense to check if the class has a Dispose() -method, in which case you use Using.
And lastly, if you try to use Using on something thats Not an IDisposable you´ll get a compiler error.
Using the Object Explorer you should be able to traverse the hierarchy to see the root of the object you're trying to use.
The compiler will warn you, though, if the variable that you're trying to use is not IDisposable:
using (int i = 1)
{
// ...
}
will give you an error:
Error 1 'int': type used in a using statement must be implicitly convertible to 'System.IDisposable'
You can check this way also
if (anyobject is IDisposable)
{
//it implemants IDisposable
}
If one can employ a "Using" statement and have it work, one generally should. One type of situation to watch out for is creating an object and passing it as a property of some other object. There are four approaches the framework can take here:
A snapshot is taken of the passed-in IDisposable. The receiving object will take care of disposing the snapshot; the supplier of the IDisposable is responsible for Disposing it, and it may do so at any time after the snapshot is taken.
A snapshot is taken of the passed-in IDisposable, without care of whether it's been disposed or not. The supplier is responsible for Disposing the IDisposable, but could legitimately do so at any time--even before it's passed in. The "Font" properties of controls seem to behave this way. If a Font object is going to be used only to set controls' Font properties, one could Dispose the font as soon as it's created and not have to worry about cleaning it up later.
The receiving object requires that the passed-in object not be Disposed until the receiving object is done with it, whereupon the receiving object will Dispose it.
The receiving object requires that the passed-in object not be Disposed until the receiving object is done with it, but the sending object is still responsible for Disposing it.
Unfortunately, Microsoft seems to use different approaches to IDisposable objects in different parts of the framework. Sometimes the best thing to do is dispose of the object immediately after setting the property, and see if that causes problems when the receiving object tries to use it.

Categories

Resources