I asked a question about this method:
// Save an object out to the disk
public static void SerializeObject(this T toSerialize, String filename)
{
If you are using native resources (for example file handles) then you should call Dispose() to close them soon, and not when the GC runs(which might be much later in higher gc generations). And you want to close the file since file access usually locks the file in some way.
A programmer thinking that one doesn't have to worry about disposing things because a finalizer will take care of them is like a driver thinking one doesn't have to worry about avoiding collisions because the car has an airbag. Yes, an airbag will make things more survivable, but...
From the TextWriter.Dispose documentation:
Note Always call Dispose before you release your last reference to the TextWriter. Otherwise, the resources it is using will not be freed until the garbage collector calls the TextWriter object's Finalize method.
From the Object.Finalize documentation:
The exact time when the finalizer executes during garbage collection is undefined. Resources are not guaranteed to be released at any specific time, unless calling a Close method or a Dispose method.
and
The Finalize method might not run to completion or might not run at all in the following exceptional circumstances:
Another finalizer blocks indefinitely (goes into an infinite loop, tries to obtain a lock it can never obtain and so on). Because the runtime attempts to run finalizers to completion, other finalizers might not be called if a finalizer blocks indefinitely.
The process terminates without giving the runtime a chance to clean up. In this case, the runtime's first notification of process termination is a DLL_PROCESS_DETACH notification.
The runtime continues to Finalize objects during shutdown only while the number of finalizable objects continues to decrease.
The garbage collector will indeed "take care of that". Sooner or later. When it gets around to calling the finalizer, after the next garbage collection.
Calling Dispose
ensures that resources (like file handles) get released as soon as possible, thereby making them available for re-use by other processes in the system. Most of the time, you won't notice the difference between calling Dispose
yourself and letting the garbage collector handle it. But there are times when you will. A Web crawler, for example, that creates a lot of HttpWebResponse
objects, will quickly run out of connections if you don't dispose them after use. (Not that I've run into that problem ... no, not me.)
In generally you must dispose objects when you do not need them anymore.
Not disposing object when you finish with them will be meaning that will block access to for other processed/applications to them.
Well actually you already are disposing it since the textWriter.Close Method does it.
public virtual void Close()
{
this.Dispose(true);
GC.SuppressFinalize(this);
}
So you could change your code to. This
public static void SerializeObject<T>(this T toSerialize, String filename)
{
TextWriter textWriter;
try
{
XmlSerializer xmlSerializer = new XmlSerializer(toSerialize.GetType());
textWriter = new StreamWriter(filename);
xmlSerializer.Serialize(textWriter, toSerialize);
}
finally
{
textWriter.Close();
}
Which is pretty similar to what the using() does in the other answers.
The impact of not doing this is that if an error occurs with Serialize it would be a while before the Framework gave up its file lock (when it Processes the fReachable queue).
I know FxCop tells you when to implment IDisposable but I don't think there's any easy way to find out when you need to call Dispose other than looking at the Docs and seeing if an object implments IDisposable (or intellisense).