In Rastertek DirectX tutorials they have empty constructors and destructors and instead use initialize()
and shutdown()
functions for objects initialization and cleaning up. After using this design for a while I can somewhat understand the benefits of having an initialize()
method, but I can't see how using a shutdown()
method is any better than putting all the clean-up code in the destructor.
The reason they provide is the following:
You will also notice I don't do any object clean up in the class destructor. I instead do all my object clean up in the Shutdown function you will see further down. The reason being is that I don't trust it to be called. Certain windows functions like ExitThread() are known for not calling your class destructors resulting in memory leaks. You can of course call safer versions of these functions now but I'm just being careful when programming on windows.
So the general usage pattern looks like this:
class Renderer
{
public:
Renderer() { }
~Renderer() { }
bool initialize(...) { /* perform initialization */ }
void shutdown() { /* clean-up */ }
};
Renderer* renderer = new Renderer;
renderer->initialize(...);
// use the renderer
renderer->shutdown();
delete renderer;
renderer = NULL;
When looking at Rastertek's code it seems to me that they're coming from C background (initializing all variables at the top of the function, using only raw pointers and raw arrays etc.), so I wonder if this is yet another thing that is unnecessary in modern C++ (for one it makes it more difficult to use smart pointers). Is there any real benefit to this design?