Suppose we have a class with a smart pointer. This class initializes a subsystem that the smart pointer relies on: similarly, this class closes the subsystem upon destruction.
If the smart pointer relies on said subsystem to free its memory, then if the destructor closes the subsystem first an issue arises.
class FontManager {
public:
FontManager() {
if(TTF_Init() < 0) {
printf( "SDL_ttf could not init! SDL_ttf Error: %s\n", TTF_GetError() );
return;
}
}
~FontManager() {
TTF_Quit();
}
std::unique_ptr<TTF_Font> font;
void operator()(TTF_Font* font) const { TTF_CloseFont(font); }
};
If I were using raw pointers the destructor would look like this.
~FontManager() {
// font is raw pointer
TTF_CloseFont(font);
TTF_Quit();
}
So, what gets called first, the pointer's destructor or the class' destructor?