I’ve been writing a lot of .NET code at work that has to Open() and Close() objects, and these objects typically provide a Dispose() method to release resources. As you know, wrapping something in a using() statement will craft a try/finally behind the scenes such that Dispose() will always get called. I had been told in years past that it’s necessary to call both Close() and Dispose() when finished with one of these objects, but didn’t understand why.
using(var foo = new ConnectionObject())
foo.Close(); //leave it out?
} //using block calls Dispose()
I told myself that I’d research it at some point in the future, and I finally found some time today.
In nearly every case, calling Close() and Dispose() will produce equivalent behavior. So why call just one or both? Typically calling Close() will close a connection and stop there, while Dispose() will both close the connection and reset the state of the connection (as well as releasing all resources). For this reason, you can call both because calling Close() twice is harmless (whereas calling Dispose() twice will blow up in your face).
So the moral of the story is to call Close() when you intend to reuse the same object state, and call Dispose() when you intend to be finished with it altogether.
Do you remember the first time you had coffee? A lot of people relate their first experience with coffee as a bad one. It’s common to hear people say that coffee is an acquired taste. For me this is untrue. I’ve always loved coffee from my very first taste, but I understand why people say it, and I want to relate that idea to C++ for a minute. C++ is a language that by more modern language standards is archaic, but it’s still arguably quite important to learn so that you understand manual memory management and how other languages are implemented.
Ok, so lets say you dive into C++ because you’re interested in knowing everything there is to know about programming. You discover it’s like your coffee experience; It’s bitter, it smells funny, and gives you jitters, but you keep on sipping it because you know it will give you benefits. Depending on your mileage, you might fast forward 6 months and find yourself practically hooking it up to your arm with an IV needle. Now that you feel comfortable with it and the knowledge it gave you, you’re ready to move on to new things. You’re satisfied that you used it as a learning tool, but are you really done with it? No; you’ll find that it’s still extremely pervasive in many areas of our industry.
And in that sense, it’s more like cocaine than coffee.
The gaming industry, or any industry that possesses an interest in high-performance computing applications, is addicted to C++. Regardless of how far managed code and garbage collection has come, they cling to it, because it’s got a lot of momentum attached to it. “JUST ONE MORE HIT MAN, JUST ONE MORE HIT”. I sympathize on the one hand because C++ ought to be mandatory learning material for computer science students (because it does teach you about how things work under the covers), but at the same time haven’t we come far enough with the performance of other languages that we can just go ahead and take C++ out back of the shed?